Aashutosh
Aashutosh

Reputation: 37

String to date time in java

I have a date time string "2013-06-04 23:59:59" I want to get the date and time separately from this string.

new Date ("2013-06-04 23:59:59") 

constructor of java has been deprecated so I used the Calender class. I have tried the code

Calendar mydate = new GregorianCalendar();
        String mystring = "2013-06-04 23:59:59";
        Date thedate = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS", Locale.ENGLISH).parse(mystring);
        mydate.setTime(thedate);
        //breakdown
        System.out.println("mydate -> "+mydate);
        System.out.println("year   -> "+mydate.get(Calendar.YEAR));
        System.out.println("month  -> "+mydate.get(Calendar.MONTH));
        System.out.println("dom    -> "+mydate.get(Calendar.DAY_OF_MONTH));
        System.out.println("dow    -> "+mydate.get(Calendar.DAY_OF_WEEK));
        System.out.println("hour   -> "+mydate.get(Calendar.HOUR));
        System.out.println("minute -> "+mydate.get(Calendar.MINUTE));
        System.out.println("second -> "+mydate.get(Calendar.SECOND));
        System.out.println("milli  -> "+mydate.get(Calendar.MILLISECOND));
        System.out.println("ampm   -> "+mydate.get(Calendar.AM_PM));
        System.out.println("hod    -> "+mydate.get(Calendar.HOUR_OF_DAY));

but the result is wrong.

mydate -> java.util.GregorianCalendar[time=1509816960059,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=10,WEEK_OF_YEAR=44,WEEK_OF_MONTH=1,DAY_OF_MONTH=4,DAY_OF_YEAR=308,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=6,SECOND=0,MILLISECOND=59,ZONE_OFFSET=19800000,DST_OFFSET=0]
year   -> 2017
month  -> 10
dom    -> 4
dow    -> 7
hour   -> 11
minute -> 6
second -> 0
milli  -> 59
ampm   -> 1
hod    -> 23

Please figure out the mistake I am doing and rectify the error.

I need the values that are mentioned in the string.

Upvotes: 1

Views: 2112

Answers (5)

acdcjunior
acdcjunior

Reputation: 135752

You got the wrong pattern. MM is for month, mm is for minutes and ss for seconds.

Use:

"yyyy-MM-dd HH:mm:ss"

Also, notice that the month is zero-based, meaning it goes from 0 to 11, not 1 to 12:

Calendar mydate = new GregorianCalendar();
String mystring = "2013-06-04 23:59:59";
Date thedate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(mystring);
mydate.setTime(thedate);
//breakdown
System.out.println("mydate -> "+mydate);
System.out.println("year   -> "+mydate.get(Calendar.YEAR));
System.out.println("month  -> "+(mydate.get(Calendar.MONTH)+1)); // <-- corrected
System.out.println("dom    -> "+mydate.get(Calendar.DAY_OF_MONTH));
System.out.println("dow    -> "+mydate.get(Calendar.DAY_OF_WEEK));
System.out.println("hour   -> "+mydate.get(Calendar.HOUR));
System.out.println("minute -> "+mydate.get(Calendar.MINUTE));
System.out.println("second -> "+mydate.get(Calendar.SECOND));
System.out.println("milli  -> "+mydate.get(Calendar.MILLISECOND));
System.out.println("ampm   -> "+mydate.get(Calendar.AM_PM));
System.out.println("hod    -> "+mydate.get(Calendar.HOUR_OF_DAY));

Upvotes: 5

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39951

Just adding my two cents here. Joda-Time is much more powerful than the built in date and time handling. It doesn't show it's full potential in an example like this but when you start doing calculations on the date it really shines.

Here is your example in Joda-Time, using the DateTimeFormatterBuilder to make the format readable and less error prone (remember the mm/MM and ss/SS mixup?)

Notice also that the month is 1 based, just as we're used to.

String myString = "2013-06-04 23:59:59";
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
        .appendYear(4, 4)
        .appendLiteral('-')
        .appendMonthOfYear(2)
        .appendLiteral('-')
        .appendDayOfMonth(2)
        .appendLiteral(' ')
        .appendHourOfDay(2)
        .appendLiteral(':')
        .appendMinuteOfHour(2)
        .appendLiteral(':')
        .appendSecondOfMinute(2)
        .toFormatter();
DateTime dateTime = dateTimeFormatter.parseDateTime(myString);

System.out.println(dateTime.year().get());
System.out.println(dateTime.monthOfYear().get());
System.out.println(dateTime.dayOfMonth().get());
System.out.println(dateTime.hourOfDay().get());
System.out.println(dateTime.minuteOfHour().get());
System.out.println(dateTime.secondOfMinute().get());

Upvotes: 1

Sam Su
Sam Su

Reputation: 6822

Both Date and Calendar are bad design in JDK and should be deprecated.Well I suggest you have a look at Joda Time or Guava.

Upvotes: 1

Lokesh Kumar
Lokesh Kumar

Reputation: 418

DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");
String mystring = "2013-06-04 23:59:59";
Date date  = formatter .parse(mystring);

 System.out.println("mydate -> "+date);
 System.out.println("year   -> "+date.get(Calendar.YEAR));

Upvotes: 2

Pradeep Pati
Pradeep Pati

Reputation: 5919

Date thedate = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS", Locale.ENGLISH).parse(mystring);

should be

Date thedate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(mystring);

Upvotes: 1

Related Questions