0

I need to convert this time and date to this timestamp format :
2019/04/22 10:04:30 to 2019-02-21T14:10:18.161+0000

this is my code, it's not working, I miss something, right?

String isoDatePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(isoDatePattern);

Date d = null;
try {
    d = simpleDateFormat.parse("2019/04/22 10:04:30");
} 
catch (ParseException e) {
    e.printStackTrace();
}

String dateString = simpleDateFormat.format(d);
Log.e("dateString ::::> ",dateString);
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
stanly Inc
  • 31
  • 6

3 Answers3

0

Given the date string you're trying to parse, you would need:

String datePattern = "yyyy/MM/dd HH:mm:ss";

For the pattern.

You probably should consider using Java 8 date/time though instead:

String str = "2019/04/22 10:04:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
java-addict301
  • 3,220
  • 2
  • 25
  • 37
0

You need two formats: the format of the date you have, to parse it, and the format of the date you want, to format it back.

kumesana
  • 2,495
  • 1
  • 9
  • 10
0

You try to parse the input with the output format. Untested:

String isoInputDatePattern = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat simpleInputDateFormat = new SimpleDateFormat(isoInputDatePattern);

String isoOutputDatePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
SimpleDateFormat simpleOutputDateFormat = new SimpleDateFormat(isoOutputDatePattern);

Date d = null;
try {
   d = simpleInputDateFormat.parse("2019/04/22 10:04:30");
} catch (ParseException e) {
   e.printStackTrace();
}

String dateString = simpleOutputDateFormat.format(d);
Log.e("dateString ::::> ",dateString);