1

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.

Aashutosh
  • 37
  • 10

5 Answers5

5

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));
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
2
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));
Lokesh Kumar
  • 420
  • 3
  • 12
1
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);
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
1

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.

Community
  • 1
  • 1
Sam Su
  • 6,532
  • 8
  • 39
  • 80
1

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());
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78