1

i convert String to Date as:

DateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
String datenow="20120917121823";
Date date2 = (Date) formatter.parse(datenow);

datenow is 12 pm. why after convert it's "Mon Sep 17 00:18:23 GMT+07:00 2012"

Can you help me? Thanks.

Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
mum
  • 1,637
  • 11
  • 34
  • 58

2 Answers2

5

Because hh in hours grabs hours from 1 to 12 you can see the encoding here. You should be using:

DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String datenow="20120917121823";
Date date2 = (Date) formatter.parse(datenow);
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
4

You can use:

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
    try {
        String temp = "Mon Sep 17 00:18:23 GMT+07:00 2012";
        Date expiry = formatter.parse(temp);
    } catch (Exception e) {
        e.printStackTrace();
    }
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61