How to Convert a Date dd/mm/yyyy to yyyy-MM-dd HH:mm:ss in Android

This article guides you through converting a date in the format dd/mm/yyyy to the standard ISO 8601 format yyyy-MM-dd HH:mm:ss in Android development.

Using SimpleDateFormat

The SimpleDateFormat class is a powerful tool for handling date formatting in Java, and thus in Android.

Steps

  1. Create a SimpleDateFormat instance with the desired input format (dd/mm/yyyy).
  2. Parse the input date string using the parse() method.
  3. Create a new SimpleDateFormat instance for the desired output format (yyyy-MM-dd HH:mm:ss).
  4. Format the parsed date using the format() method.

Code Example


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

public class DateConverter {

    public static void main(String[] args) {
        String inputDate = "25/12/2023";

        try {
            // Input format (dd/mm/yyyy)
            SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
            Date date = inputFormat.parse(inputDate);

            // Output format (yyyy-MM-dd HH:mm:ss)
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            String formattedDate = outputFormat.format(date);

            System.out.println("Input Date: " + inputDate);
            System.out.println("Formatted Date: " + formattedDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Input Date: 25/12/2023
Formatted Date: 2023-12-25 00:00:00

Using Calendar

The Calendar class offers a more flexible way to handle dates and times. It provides a mechanism to manipulate individual date components.

Steps

  1. Create a Calendar instance and set it to the current time zone.
  2. Parse the input date string into year, month, and day components.
  3. Set the Calendar instance’s year, month, and day fields using the parsed values.
  4. Retrieve the formatted date string from the Calendar instance using the get() method.

Code Example


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

public class DateConverter {

    public static void main(String[] args) {
        String inputDate = "25/12/2023";

        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
            Date date = inputFormat.parse(inputDate);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeZone(java.util.TimeZone.getDefault());
            calendar.setTime(date);

            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1; // Month is 0-indexed
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            String formattedDate = String.format("%04d-%02d-%02d %02d:%02d:%02d",
                    year, month, day, 0, 0, 0);

            System.out.println("Input Date: " + inputDate);
            System.out.println("Formatted Date: " + formattedDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Input Date: 25/12/2023
Formatted Date: 2023-12-25 00:00:00

Comparison

Feature SimpleDateFormat Calendar
Ease of Use Simpler for basic formatting More flexible for complex manipulation
Performance Generally faster for simple formatting Can be slower for complex operations
Flexibility Limited to date formatting Allows manipulating individual date components

Conclusion

Both SimpleDateFormat and Calendar provide effective ways to convert a date in dd/mm/yyyy format to yyyy-MM-dd HH:mm:ss in Android. Choose the method that best suits your specific requirements and development context.

Leave a Reply

Your email address will not be published. Required fields are marked *