Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a date dd / mm / yyyy to yyyy-MM-dd HH:mm:ss Android [duplicate]

How can I convert a date in dd / mm / yyyy to a format that supports sqlite yyyy-MM-dd'T'HH: mm: ss

for example:

public static String convertStringToData(String stringData)
        throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/aaaa");//yyyy-MM-dd'T'HH:mm:ss
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date data = sdf.parse(stringData);
    String formattedTime = output.format(data);
    return formattedTime;
}
like image 630
Douglas Mesquita Avatar asked May 07 '13 19:05

Douglas Mesquita


People also ask

How do I change the date format from mm/dd/yyyy to mm/dd/yyyy in Java?

Format date with SimpleDateFormat('MM/dd/yy') in Java // displaying date Format f = new SimpleDateFormat("MM/dd/yy"); String strDate = f. format(new Date()); System. out. println("Current Date = "+strDate);

What is SSSZ in date format?

Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.


2 Answers

public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {

    Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
    SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
    String parsedDate = formatter.format(initDate);

    return parsedDate;
}

This will return the parsed date as a String, with the format (both initial and end) as parameters to the method.

like image 166
fcm Avatar answered Sep 22 '22 06:09

fcm


   SimpleDateFormat originalFormat = new SimpleDateFormat("dd MM yyyy");
   SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy MM dd HH:mm:ss" );
   Date date;
   try {
     date = originalFormat.parse("21 6 2013");
     System.out.println("Old Format :   " + originalFormat.format(date));
     System.out.println("New Format :   " + targetFormat.format(date));

    } catch (ParseException ex) {
      // Handle Exception.
    }

Old Format : 21 06 2013

New Format : 2013 06 21 00:00:00

like image 42
Raghunandan Avatar answered Sep 22 '22 06:09

Raghunandan