Code to Convert dd-MMM-yy to dd-MM-yyyy in Android Java

Jyotishgher Astrology
By -
0

 Code to Convert dd-MMM-yy to dd-MM-yyyy in Android Java

To convert workDate for example from format "15-JUL-25" to "15-07-2025" in Java, you can use SimpleDateFormat as shown below:

Code to Convert dd-MMM-yy to dd-MM-yyyy in Android Java

Method 1

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

String originalFormat = “dd-MMM-yy”; // Input format: 15-JUL-25
String targetFormat = “dd-MM-yyyy”; // Output format: 15–07–2025

SimpleDateFormat inputFormat = new SimpleDateFormat(originalFormat, Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat(targetFormat, Locale.ENGLISH);

try {
 Date date = inputFormat.parse(workDate); // workDate = “15-JUL-25”
 String formattedDate = outputFormat.format(date); // formattedDate = “15–07–2025”

jsonObject.put(“workDt”, formattedDate); // ✅ Put formatted date in JSON
} catch (ParseException e) {
 e.printStackTrace();
 jsonObject.put(“workDt”, workDate); // Fallback to original if parsing fails
}

Explanation:

  • "dd-MMM-yy" parses input like "15-JUL-25" (case-insensitive with Locale.ENGLISH)
  • "dd-MM-yyyy" formats output like "15-07-2025"
  • Safe fallback is added in case parsing fails

Method 2

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateConverter {

public static String convertDateFormat(String dateString) {
 // Input format: dd-MMM-yy (e.g., 15-Jul-25)
 SimpleDateFormat inputFormat = new SimpleDateFormat(“dd-MMM-yy”, Locale.ENGLISH);

// Output format: dd-MM-yyyy (e.g., 15–07–2025)
 SimpleDateFormat outputFormat = new SimpleDateFormat(“dd-MM-yyyy”, Locale.getDefault());

try {
 // Parse the input date string into a Date object
 Date date = inputFormat.parse(dateString);

// Format the Date object into the desired output format
 return outputFormat.format(date);
 } catch (ParseException e) {
 e.printStackTrace();
 // Handle parsing error, e.g., return null or throw an exception
 return null;
 }
 }

public static void main(String[] args) {
 // Example usage:
 String dateToConvert = “15-Jul-25”; // Current year is 2025
 String convertedDate = convertDateFormat(dateToConvert);

if (convertedDate != null) {
 System.out.println(“Original Date: “ + dateToConvert);
 System.out.println(“Converted Date: “ + convertedDate);
 } else {
 System.out.println(“Date conversion failed.”);
 }

String anotherDate = “01-Jan-01”; // Test with year 2001
 String convertedAnotherDate = convertDateFormat(anotherDate);
 if (convertedAnotherDate != null) {
 System.out.println(“Original Date: “ + anotherDate);
 System.out.println(“Converted Date: “ + convertedAnotherDate);
 } else {
 System.out.println(“Date conversion failed.”);
 }
 }
}

You can convert a date string from “dd-MMM-yy” to “dd-MM-yyyy” in Android Java using SimpleDateFormat. Here's the code:

Java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateConverter {
    public static String convertDateFormat(String dateString) {
// Input format: dd-MMM-yy (e.g., 15-Jul-25)
SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
        // Output format: dd-MM-yyyy (e.g., 15-07-2025)
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
        try {
// Parse the input date string into a Date object
Date date = inputFormat.parse(dateString);
            // Format the Date object into the desired output format
return outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
// Handle parsing error, e.g., return null or throw an exception
return null;
}
}
    public static void main(String[] args) {
// Example usage:
String dateToConvert = "15-Jul-25"; // Current year is 2025
String convertedDate = convertDateFormat(dateToConvert);
        if (convertedDate != null) {
System.out.println("Original Date: " + dateToConvert);
System.out.println("Converted Date: " + convertedDate);
} else {
System.out.println("Date conversion failed.");
}
        String anotherDate = "01-Jan-01"; // Test with year 2001
String convertedAnotherDate = convertDateFormat(anotherDate);
if (convertedAnotherDate != null) {
System.out.println("Original Date: " + anotherDate);
System.out.println("Converted Date: " + convertedAnotherDate);
} else {
System.out.println("Date conversion failed.");
}
}
}

Explanation:

  • SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
  • This line creates a SimpleDateFormat object to parse your input date string.
  • "dd-MMM-yy" is the pattern for your input format:
  • dd: Day of the month (01-31)
  • MMM: Abbreviated month name (Jan, Feb, Jul, etc.)
  • yy: Two-digit year (e.g., 25 for 2025)
  • Locale.ENGLISH is crucial when dealing with month abbreviations (like "Jul") because month names can vary by locale. Using Locale.ENGLISH ensures that "Jul" is correctly interpreted as July.
  • SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
  • This creates another SimpleDateFormat object for the desired output format.
  • "dd-MM-yyyy" is the pattern for your output format:
  • dd: Day of the month (01-31)
  • MM: Month in two digits (01-12)
  • yyyy: Four-digit year (e.g., 2025)
  • Locale.getDefault() is generally fine for output unless you have specific locale requirements for the formatted string.
  • Date date = inputFormat.parse(dateString);
  • The parse() method attempts to convert the dateString (e.g., "15-Jul-25") into a java.util.Date object according to the inputFormat.
  • This is where the two-digit year (yy) is expanded into a four-digit year. SimpleDateFormat uses a default century for two-digit years (typically 80 years before the current date to 20 years after). For example, "25" would become 2025, and "90" might become 1990.
  • return outputFormat.format(date);
  • The format() method takes the Date object and converts it into a string representation using the outputFormat ("dd-MM-yyyy").
  • try-catch (ParseException e):
  • Parsing a date string can fail if the string doesn’t match the expected format. The try-catch block handles ParseException to prevent your app from crashing. You should implement appropriate error handling (e.g., showing a toast message, logging the error, or returning a default value) in a real Android application.

How to use it in Android:

You can place the convertDateFormat method in a utility class or directly within your Activity/Fragment where you need to perform the conversion.

// In your Activity or Fragment:
String originalDate = “15-Jul-25”;
String newFormattedDate = DateConverter.convertDateFormat(originalDate);
if (newFormattedDate != null) {
 // Use newFormattedDate, e.g., set it to a TextView
 myTextView.setText(newFormattedDate);
} else {
 // Handle the error
 Toast.makeText(this, “Error converting date”, Toast.LENGTH_SHORT).show();
}



Tags:

Post a Comment

0Comments

Post a Comment (0)