java中的字符串到日期时间

时间:2013-06-08 05:12:35

标签: java date datetime

我有一个日期时间字符串“2013-06-04 23:59:59”我想从这个字符串中分别得到日期和时间。

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

java的构造函数已被弃用,因此我使用了Calender类。我试过了代码

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));

但结果是错误的。

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

请弄清楚我正在做的错误并纠正错误。

我需要字符串中提到的值。

5 个答案:

答案 0 :(得分:5)

你的模式错了。 MM为月,mm为分钟,ss为秒。

使用:

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

另请注意,月份从零开始,这意味着它从011,而不是112

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));

答案 1 :(得分: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));

答案 2 :(得分:1)

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

应该是

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

答案 3 :(得分:1)

DateCalendar都是bad design in JDK,应该弃用。我建议您查看Joda TimeGuava

答案 4 :(得分:1)

在这里加我的两分钱。 Joda-Time比内置的日期和时间处理功能强大得多。它并没有在这样的例子中显示出它的全部潜力,但是当你开始在日期进行计算时它真的很闪耀。

以下是Joda-Time中的示例,使用DateTimeFormatterBuilder使格式可读且不易出错(请记住mm / MM和ss / SS混音?)

另请注意,月份是1,正如我们习惯的那样。

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());