Java日历 - 获取小时和分钟

时间:2014-05-21 12:52:19

标签: java calendar

我想从DB获取日期并从日期年,月,日,小时和分钟中获取

我这样做的方式是:

Date dateStart = calendarEvents.getDateStart();

        java.util.Calendar calendarEventStart = java.util.Calendar.getInstance();
        calendarEventStart.setTime(dateStart);

        java.util.Calendar now = java.util.Calendar.getInstance();

        parseDateToCalendar.setYearStart(now.get(calendarEventStart.YEAR));
        parseDateToCalendar.setMonthStart(now.get(calendarEventStart.MONTH));
        parseDateToCalendar.setDayStart(now.get(calendarEventStart.DAY_OF_MONTH));
        parseDateToCalendar.setHourStart(now.get(calendarEventStart.HOUR_OF_DAY));
        parseDateToCalendar.setMonthStart(now.get(calendarEventStart.MINUTE));

java让我感到惊讶,在DB中我的日期是:

2014-05-21 04:15:00.0

dateStart变量也有日期

2014-05-21 04:15:00.0

now.get(calendarEventStart.HOUR_OF_DAY显示:14

now.get(calendarEventStart.MINUTE)显示:46

为什么以及如何才能获得正确的小时和分钟?

你是对的 - 现在是正确的日期,但现在我有一个问题:

        java.util.Calendar calendarEventStart = GregorianCalendar.getInstance();
        calendarEventStart.setTime(dateStart);

        parseDateToCalendar.setYearStart(calendarEventStart.get(calendarEventStart.YEAR));
        parseDateToCalendar.setMonthStart(calendarEventStart.get(calendarEventStart.MONTH));
        parseDateToCalendar.setDayStart(calendarEventStart.get(calendarEventStart.DAY_OF_MONTH));
        parseDateToCalendar.setHourStart(calendarEventStart.get(calendarEventStart.HOUR_OF_DAY));
        parseDateToCalendar.setMonthStart(calendarEventStart.get(calendarEventStart.MINUTE));

显示:

parseDateToCalendar.getYearStart() = 2014 - THIS IS OK (YEAR)
parseDateToCalendar.getMonthStart() = 30 - !!WRONG - THIS SHOULD BE MINUTE
parseDateToCalendar.getDayStart() = 21 - THIS IS OK (DAY)
parseDateToCalendar.getHourStart() = 4 - !!WRONG - THIS SHOULD BE MONTH
parseDateToCalendar.getMinuteStart() = 0 - !!WRONG - HERE SHOULD BE VALUE FROM MONTH

2 个答案:

答案 0 :(得分:1)

您从现在开始收到信息,而不是从您检索到的信息。             Date dateStart = calendarEvents.getDateStart();

        java.util.Calendar calendarEventStart = java.util.Calendar.getInstance();
        calendarEventStart.setTime(dateStart);

        parseDateToCalendar.setYearStart(calendarEventStart .get(calendarEventStart.YEAR));
        parseDateToCalendar.setMonthStart(calendarEventStart .get(calendarEventStart.MONTH));
        parseDateToCalendar.setDayStart(calendarEventStart .get(calendarEventStart.DAY_OF_MONTH));
        parseDateToCalendar.setHourStart(calendarEventStart .get(calendarEventStart.HOUR_OF_DAY));
        parseDateToCalendar.setMonthStart(calendarEventStart .get(calendarEventStart.MINUTE));

答案 1 :(得分:1)

现在是实际日期!不是DB的日期