1

I am trying to convert date from one format to another using formatter. But somehow this does not see to be working.

Following is my code

Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.ms");
String folderName = formatter.format(today); 
Date date = formatter.parse(folderName);
System.out.println("Folder date = " + date); //This prints Wed Nov 09 06:05:57 IST 2016

I need date to be printed in yyyy-MM-dd hh:mm:ss.ms format. That is I need it to be 2016-11-09 06:50:25.5025 and not Wed Nov 09 06:05:57 IST 2016 In folderName it is in correct format but when I convert back to date again format changes. Could you please let me know what I am missing?

Sachin
  • 247
  • 4
  • 16
  • 26
  • 2
    "ms" ?? try "SSS" ! See https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html – Fildor Nov 09 '16 at 13:17
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 18 '17 at 00:53

2 Answers2

3

You did two errors:

The format (check here for a complete list of formats) is yyyy-MM-dd hh:mm:ss.SSS

And you need to print the String, not the Date.

Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
String folderName = formatter.format(today); 
System.out.println("Folder date = " + folderName); 
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

Using java.time

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;

now.toString(): 2016-11-09T06:50:25.502+05:30[Asia/Kolkata]

Generate a string. Your desired format is close to standard ISO 8601 format, just replace the T in the middle with a space. The java.time classes know about ISO 8601 formats.

DateTimeFormatter f = DateTimeFormatter.ISO_LOCAL_DATE_TIME ;
String output = now.format( f ).replace( "T" , " " ) ;

2016-11-09 06:50:25.502


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154