-1

I want to convert String into Date in Java. I have written following code for that:

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSSSSS");       

Date date = dateformat.parse("2015-07-16 23:59:59.0");

dateformat.format(date);

After parsing I am getting following value for date : Fri Jan 16 23:59:59 IST 2015

I have tried many examples but didn't get proper solution.

Thanks in advance.

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Nitesh
  • 1,477
  • 5
  • 23
  • 34
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – triForce420 Jul 15 '15 at 16:27
  • Please search StackOverflow before posting. You could have copied working example code from any of hundreds of existing Questions and Answers instead of posting this duplicate Question. – Basil Bourque Jul 15 '15 at 18:07
  • @Basil: I have searched a lot on stackoverflow. also I have tried many examples. My intention was to get the solution of my problem, and was not posting duplicate question. – Nitesh Jul 16 '15 at 06:17

2 Answers2

5

"mm" in the format string stands for minutes. You need to specify "MM" for the month part, or it defaults to January:

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS"); 
Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36
0

As you said you want to convert string to date then it will be received only in that format.

We cant format date in java we can format string representation of date using dateforamtter but if you are converting string to date it will be received in only that format that will be unformatted.

kirti
  • 4,499
  • 4
  • 31
  • 60