1

我有一个日期时间字符串“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

请找出我正在做的错误并纠正错误。

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

4

5 回答 5

5

你有错误的模式。MM是月份,mm是分钟和ss秒。

采用:

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

另外,请注意月份是从零开始的,这意味着它从0to 开始11,而不是1to 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));
于 2013-06-08T05:16:58.523 回答
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));
于 2013-06-08T05:24:46.030 回答
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);
于 2013-06-08T05:17:14.900 回答
1

两者DateCalendar都是JDK 中的糟糕设计,应该被弃用。好吧,我建议你看看Joda TimeGuava

于 2013-06-08T05:30:49.060 回答
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());
于 2013-06-08T06:22:36.283 回答