2

log(2 different dates):

START TIME BEFORE PARSE: 06/27/2012 09:00
START TIME AFTER PARSE : Thu Mar 06 09:00:00 EET 2014


START TIME BEFORE PARSE: 07/06/2012 09:00
START TIME AFTER PARSE : Thu Jun 07 09:00:00 EEST 2012

code :

DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            Date date = sdf.parse(time);
            System.out.println("TIME BEFORE PARSE: " + time);
            System.out.println("TIME AFTER PARSE : " + date);

Why does it mess up the year? How to get it to work?

2
  • @Nishant : im printing the time out in log Commented Jul 10, 2012 at 6:59
  • It does mess up the year in which way ? Do you have an output for that ? Commented Jul 10, 2012 at 7:00

5 Answers 5

6

Because you inverted the month with the date:

              dd/MM/yyyy HH:mm
              06/27/2012 09:00

There is not 27th month in a year.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, but what I want, is the dd/MM/yyyy HH:mm format, can I somehow reverse it, like I tried to?
2

The month in the first example is 27 which isn't valid in any calendar I'm aware of. (You probably just got the day/month ordering wrong, either on your input, or in the format you've chosen.)

1 Comment

Ok, but what I want, is the dd/MM/yyyy HH:mm format, can I somehow reverse it, like I tried to?
2

You use the pattern dd/MM/yyyy to parse the date 06/27/2012. I doubt 27 is a month. The appropriate format is MM/dd/yyyy.

The DateFormat is lenient by default, and will thus consider 27 as a valid month: 2 years + 3 months, so you end up in March, 2 years later.

2 Comments

Ok, but what I want, is the dd/MM/yyyy HH:mm format, can I somehow reverse it, like I tried to?
Your question makes no sense: you have a String representing a date in the format MM/dd/yyyy. It's like that, and you have no choice. If the string doesn't have the appropriate format, fix the code where this string is produced, not the code where it's parsed.
1
    String time = "06/27/2012 09:00";
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    Date date = sdf.parse(time);
    System.out.println("TIME BEFORE PARSE: " + time);
    System.out.println("TIME AFTER PARSE : " + date);

In your example date format is wrong. You have give "dd/MM/yyyy HH:mm" which should be "MM/dd/yyyy HH:mm"

Comments

0

You have used the pattern dd/MM/YYYY , but you have entered the date as MM/dd/YYYY, causing you this weird behaviour..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.