0

I am facing weird behavior with "yyyy-MM-dd hh:mm:ss" date parsing pattern. below is my code.

    public static void main(String[] args) throws Exception {
        System.out.println(parseDate("2018-08-16 11:00:00"));
        System.out.println(parseDate("2018-08-16 12:00:00"));
        System.out.println(parseDate("2018-08-16 13:00:00"));
    }

    public static Date parseDate(String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.parse(date);
    }

and I am getting following output

Thu Aug 16 11:00:00 UTC 2018

Thu Aug 16 00:00:00 UTC 2018

Thu Aug 16 13:00:00 UTC 2018

I don't understand the second output why for "2018-08-16 12:00:00" its saying Thu Aug 16 00:00:00 UTC 2018 instead of Thu Aug 16 12:00:00 UTC 2018.

looking at the docs for date parsing pattern from here. It says

h -> Hour in am/pm (1-12)

can someone please explain this? Thanks in anticipation.

sumit
  • 3,210
  • 1
  • 19
  • 37
  • 6
    Without the `am`/`pm` value, what is `12`? I'd also suggest that you should be using the new date/time introduced in Java 8 – MadProgrammer Aug 17 '18 at 05:49
  • try introducing am/pm – Scary Wombat Aug 17 '18 at 05:49
  • 2
    Or change your formatter to use `H` instead – MadProgrammer Aug 17 '18 at 05:52
  • Also, if you use the `SimpleDateFormat` (and the same format) to `format` the result, it will print `2018-08-16 12:00:00` :/ - Remember, `Date#toString` has it's own internal mechanism for formatting it's representation – MadProgrammer Aug 17 '18 at 06:04
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 17 '18 at 08:27
  • As Ole V.V. commented, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/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/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 17 '18 at 22:23

3 Answers3

0

Hope this helps

public static void main(String[] args) throws Exception {
    System.out.println(parseDate("2018-08-16 11:00:00 AM"));
    System.out.println(parseDate("2018-08-16 12:00:00 PM"));
    System.out.println(parseDate("2018-08-16 13:00:00 AM"));
}

public static Date parseDate(String date) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
    return sdf.parse(date);
}

Output

Thu Aug 16 11:00:00 IST 2018 Thu Aug 16 12:00:00 IST 2018 Thu Aug 16 13:00:00 IST 2018

Samy
  • 2,387
  • 2
  • 17
  • 31
0

You should use H instead hh for Hour in day (0-23) :

TestP:

public class TestP{
    public static void main(String[] args) throws Exception {
        System.out.println(parseDate("2018-08-16 11:00:00"));
        System.out.println(parseDate("2018-08-16 12:00:00"));
        System.out.println(parseDate("2018-08-16 13:00:00"));
        System.out.println(parseDate("2018-08-16 23:00:00"));
    }

    public static Date parseDate(String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
        return sdf.parse(date);
    }
}

Output:

Thu Aug 16 11:00:00 CLST 2018
Thu Aug 16 12:00:00 CLST 2018
Thu Aug 16 13:00:00 CLST 2018
Thu Aug 16 23:00:00 CLST 2018

Docs: SimpleDateFormat

Rcordoval
  • 1,932
  • 2
  • 19
  • 25
  • `AddRotatedAnnotation`?? – Basil Bourque Aug 17 '18 at 22:22
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/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/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 17 '18 at 22:22
0

You have change bellow line

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

to

DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

Hours date format like below,

kk :- Hours in 1-24 format
hh :- hours in 1-12 format
KK :- hours in 0-11 format
HH :- hours in 0-23 format
Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65