简体   繁体   English

日历添加方法无法正常工作

[英]Calendar add method not working as expected

I want set a (Gregorian) Calendar time to start-of-day April 1, 2016. I do this by setting month, day and year, and then setting all the time fields to their minimum values, giving me: 我想将(Gregorian)日历时间设置为2016年4月1日开始。我可以通过设置月,日和年,然后将所有时间字段设置为其最小值来实现:

04/01/2016 12:00:00:000 04/01/2016 12:00:00:000

If I subtract one millisecond I expect to get the last millisecond on the previous day, but instead I get the last millisecond on the same day: 如果我减去一毫秒,我希望得到前一天的最后一个毫秒,但是我却会在同一天得到最后的毫秒:

04/01/2016 11:59:59:999 2016年4月1日11:59:59:999

(Note: I get similar results if I subtract one second, minute or hour.) (注意:如果减去一秒钟,一分钟或一小时,我会得到类似的结果。)

What am I missing? 我想念什么? Sample code follows, thanks. 示例代码如下,谢谢。 package com.scg.domain; 包com.scg.domain;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class AdHoc
{
    private static final int[]  UNUSED_CAL_FIELDS   =
    {
        Calendar.HOUR,
        Calendar.MINUTE,
        Calendar.SECOND,
        Calendar.MILLISECOND
    };

    public static void main(String[] args)
    {
        Calendar    cal = getGoodDate( Calendar.APRIL, 1,2016 );
        System.out.println( cal.getCalendarType() );
        adjustTime( "present", cal, 0 );
        adjustTime( "past", cal, -1 );
        adjustTime( "future", cal, 1 );
    }

    private static void adjustTime( String comment, Calendar cal, int incr )
    {
        Calendar    newCal  = Calendar.getInstance();
        newCal.setTime( cal.getTime() );
        newCal.add( Calendar.MILLISECOND, incr );

        SimpleDateFormat    fmt =
            new SimpleDateFormat( "MM/dd/YYYY HH:mm:ss:SSS" );
        System.out.println( comment + ": " + fmt.format( newCal.getTime() ) );
    }

    private static Calendar getGoodDate( int month, int day, int year )
    {
        Calendar    cal = Calendar.getInstance();

        cal.set( Calendar.YEAR, year );
        cal.set( Calendar.MONTH, month );
        cal.set( Calendar.DAY_OF_MONTH, day );

        for ( int field : UNUSED_CAL_FIELDS )
            cal.set( field, cal.getMinimum( field ) );

        return cal;
    }
}

You've used Calendar.HOUR , which controls only the 1-12 hour, not the 0-23 hour. 您已使用Calendar.HOUR ,它仅控制1-12小时,而不控制0-23小时。 Because of this, even though getMinimum returns 0 , it's interpreted as 12:00 in whichever of AM or PM cal already is in, which must be PM from getInstance() (returns "now"). 因此,即使getMinimum返回0 ,也将其解释为12:00,无论AM或PM cal已在其中,它必须是getInstance() PM(返回“ now”)。 You really have 12:00 noon (PM). 您确实是中午12:00(PM)。 When you subtract a millisecond, it's still the same day. 当您减去毫秒时,仍是同一天。

To set it to midnight, try Calendar.HOUR_OF_DAY , which controls the 0-23 hour. 要将其设置为午夜,请尝试Calendar.HOUR_OF_DAY ,它控制0-23小时。

With this change, I get the output: 通过此更改,我得到了输出:

gregory
present: 04/01/2016 00:00:00:000
past: 03/31/2016 23:59:59:999
future: 04/01/2016 00:00:00:001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM