1

I am trying to parse 14th March 2011 to a date in Java for a time converter application... I get 26th Dec 2010... Please help.

import java.util.*;
import java.text.*;

class date {
    public static void main(String[] args) {
      try {
        String timestampOrig = "11/03/14,15:00:00";
        SimpleDateFormat inFormat = new SimpleDateFormat("YY/MM/dd','HH:mm:ss");
        Date parseDate = inFormat.parse(timestampOrig);
        System.out.println("parsed date: " + parseDate.toString());
      }
      catch(ParseException pe){
      }
    }
}

output:

parsed date: Sun Dec 26 15:00:00 EST 2010

GreenDroid
  • 157
  • 1
  • 2
  • 15
  • 1
    you'll get the 14th March with that datestring - may is 05 not 03 – Rachel Gallen Jan 27 '13 at 11:13
  • @RachelGallen editted accordingly... thanks – GreenDroid Jan 27 '13 at 11:17
  • 1
    FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. See modern solution in [Answer by Ole V.V.](https://stackoverflow.com/a/57833510/642706) – Basil Bourque Sep 07 '19 at 19:29

2 Answers2

3

YY should be yy (in lower case). You can find the list of available characters and their meaning in the documentation.

Out of curiosity, more information about YY, which is for week year, here (not 100% sure what it is to be honest).

Anthony Grist
  • 37,428
  • 8
  • 62
  • 73
assylias
  • 297,541
  • 71
  • 621
  • 741
  • Yeah... so much for case sensitivity!!! ...wasted almost 4-5 hours on something I dont understand... but it works... I am happy :) – GreenDroid Jan 27 '13 at 11:19
  • 2
    FYI, a *week-based year* is an alternate kind of calendar focused on tracking whole weeks rather than January 1 to December 31. Definitions of a week vary. One definition might be that a week starts on the first Sunday on or before January 1. So that week based year might start on December 29th of the prior calendar year. For more info, read about the standard [ISO 8601 week](https://en.wikipedia.org/wiki/ISO_week_date) where week # 1 begins on a Monday and contains the first Thursday of the calendar year, so every year has exactly 52 or 53 weeks with each week having a full 7 days. – Basil Bourque Sep 07 '19 at 19:34
1

java.time

I am providing the modern answer using java.time, the modern Java date and time API (since March 2014).

    DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("uu/MM/dd,HH:mm:ss");
    String timestampOrig = "11/03/14,15:00:00";
    LocalDateTime parsedDateTime = LocalDateTime.parse(timestampOrig, inFormat);
    System.out.println("parsed date: " + parsedDateTime.toString());

Output is:

parsed date: 2011-03-14T15:00

I recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use LocalDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API. The modern API is so much nicer to work with. And BTW would throw an exception if you tried with YY for year, which I find somewhat more helpful for catching your error.

The quotes around the comma in the format pattern string are optional. I know that the documentation recommends them, but I find the format pattern string more readable without them, so left them out.

What went wrong in your code?

Uppercase Y in the format patterns string is for week based year and only useful with a week number. Apperently SimpleDateFormat wasn’t able to combine your specified month and day of month with the week based year of 2011 and instead just took the first day of the week-based year (this is typical behaviour of SimpleDateFormat, giving you a result that cannot be but wrong and pretending all is well). Assuming your locale is American or similar, week 1 is the week that contains January 1 and begins on the Sunday of the same week, therefore in this case the last Sunday of the previous year, December 26, 2010.

With java.time you may use either lowercase y or u for year. The subtle difference is explained in the question linked to at the bottom. In any case a two-digit year is interpreted into the range from year 2000 through 2099 (there are ways to control the interpretation if you need to).

Links

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117