10

my current date format is : 08/11/2008 00:00

I need to convert this output to 2008/11/08 00:00 However, using the SimpleDateFormat as researched it is unable to do so and give me a totally different output, here are my codes as Follows :

SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
Date starting= simpleDateFormat2.parse(startTime);
System.out.println("" + simpleDateFormat2.format(starting) + " real date " + startTime);

i do know that i am parsing in the right string given that the following output occurs :

0014/05/01 00:00 real date 08/11/2008 00:00

i am not too sure about how as to the mechanics detected 0014/05/01 00:00 instead of

2008/11/08 00:00

i look forward to all sugguestions Thanks in advance

Cosq
  • 155
  • 1
  • 2
  • 12
  • thanks guys! for the suggestions!!@madprogrammer yes this truly works ! another thing i found out too is the use of .setLenient to false but yours works betters – Cosq Jan 18 '16 at 02:31

4 Answers4

13

The first thing you need to do is parse the original value to a Date object

String startTime = "08/11/2008 00:00";
// This could be MM/dd/yyyy, you original value is ambiguous 
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date dateValue = input.parse(startTime);

Once you have that done, you can format the dateValue any way you want...

SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("" + output.format(dateValue) + " real date " + startTime);

which outputs:

2008/11/08 00:00 real date 08/11/2008 00:00

The reason you're getting 0014/05/01 00:00 is SimpleDateFormat (when using yyyy/MM/dd HH:mm) is using 08 for the year, 11 for the month and 2008 for the day, it's doing an internal rolling of the values to correct the values to a valid date

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
5

I should like to contribute the modern answer.

    DateTimeFormatter currentFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm");
    DateTimeFormatter convertedOutputFormatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm");
    String startTime = "08/11/2008 00:00";
    LocalDateTime starting = LocalDateTime.parse(startTime, currentFormatter);
    System.out.println(starting.format(convertedOutputFormatter));

This prints

2008/11/08 00:00

The SimpleDateFormat class you tried to use is long outdated and notoriously troublesome. You have experienced but a little of the trouble with it. Instead I am using and recommending java.time, the modern Java date and time API.

As others have said, you need to use two formatters, one for specifying your current format and one for specifying the desired format.

What went wrong in your code?

Your formatter with format pattern yyyy/MM/dd HH:mm interpreted 08/11/2008 as the 2008th day of the 11th month of year 8 of the common era. It doesn’t disturb a SimpleDateFormat with default settings that there are only 30 days in November, it just continues counting into the following months and years, ending up on a date five and a half years later (2008 divided by 365.25 is very close to 5.5).

Question: Can I use java.time with my Java version?

If using at least Java 6, you can.

  • In Java 8 and later the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
4

It is because the source date string you parsed is in a different format than your SimpleDateFormatter. Your source is in MM/dd/yyyy HH:mm format. Change the string in the SimpleDateFormat to MM/dd/yyyy HH:mm.

Try using another format after your first format.

SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
//You gotta parse it to a Date before correcting it
Date parsedDate = simpleDateFormat2.parse(startTime);
simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
String newFormatttedDate = simpleDateFormat2.format(parsedDate);
Enzo Beltrami
  • 81
  • 1
  • 12
MetaSnarf
  • 5,857
  • 3
  • 25
  • 41
0

You're parsing the date 08/11/2008 00:00 with the format yyyy/MM/dd HH:mm but this date is not in this format.

tkausl
  • 13,686
  • 2
  • 33
  • 50