By Clement Ong

2016-01-18 02:24:26 8 Comments

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


@MetaSnarf 2016-01-18 02:27:39

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);

@Meinkraft 2016-01-18 02:28:04

I like how your reputation said 365 :)

@MetaSnarf 2016-01-18 02:29:03

Yeah,like a year..just noticed it.thanks for pointing it out..ahha

@MadProgrammer 2016-01-18 02:30:37

Or dd/MM/yyyyy HH:mm, the actual String value is ambiguous

@Ole V.V. 2018-01-17 11:08:27

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);

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.


@MadProgrammer 2016-01-18 02:29:02

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

@tkausl 2016-01-18 02:26:41

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.

Related Questions

Sponsored Content

56 Answered Questions

[SOLVED] How do I get the current date in JavaScript?

38 Answered Questions

[SOLVED] Compare two dates with JavaScript

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

57 Answered Questions

[SOLVED] How to format a JavaScript date

44 Answered Questions

[SOLVED] Detecting an "invalid date" Date instance in JavaScript

  • 2009-08-30 11:34:40
  • orip
  • 853699 View
  • 1537 Score
  • 44 Answer
  • Tags:   javascript date

15 Answered Questions

[SOLVED] Is there any way to change input type="date" format?

  • 2011-09-10 13:33:36
  • Tural Ali
  • 1201766 View
  • 780 Score
  • 15 Answer
  • Tags:   html css date input

17 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

13 Answered Questions

[SOLVED] YYYY-MM-DD format date in shell script

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content