0

i'm trying to convert my date which is in string form to date

my code :

String dtStart = passedDate+" "+passedTme; // here my time is : 28/08/16 2:00 pm
        System.out.println(dtStart);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm a");
        try {
            Date date = format.parse(dtStart); 
            System.out.println(date); // and result output is : Fri Aug 28 00:00:00 GMT+05:30 16
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

the above is taking 28/08/16 2:00 pm and returning Fri Aug 28 00:00:00 GMT+05:30 16.

anyone can point out why my output is not correct here ??

SOLVED required format was this : SimpleDateFormat("dd/MM/yy h:mm a").

remy boys
  • 2,928
  • 5
  • 36
  • 65

2 Answers2

2

anyone can point out why my output is not correct here

You set yyyy but you passed 16 as a year. Either pass 4 digit year or fix your pattern.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Firstly it would be helpful to know what kind of format do you need for the output but here is a list of examples

Date and Time Pattern           Result
"yyyy.MM.dd G 'at' HH:mm:ss z"   2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"           Wed, Jul 4, '01
"h:mm a"                   12:08 PM
"hh 'o''clock' a, zzzz"           12 o'clock PM, Pacific Daylight Time
"K:mm a, z"                   0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"   02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"   Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"                   010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"   2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"   2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u"                   2001-W27-3

You can read more about it here

Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45