gpt4 book ai didi

java - 比较时间戳时,年份转到 1970

转载 作者:行者123 更新时间:2023-11-30 10:55:26 25 4
gpt4 key购买 nike

我需要比较当前时间是否在 X+30X-30 之间。

时间戳一切正常,但比较年份设置为 1970 年;所以输出不准确。

String string1 = "04:41 PM";
Calendar calendar1,calendar2,calendar3,calendar4 ;
DateFormat time1;
Date time;
try {
time1 =new SimpleDateFormat("hh:mm a");
time = time1.parse(string1);
calendar1 = Calendar.getInstance();
calendar1.setTime(time);
System.out.println(time1.format(calendar1.getTime()));
calendar2 = Calendar.getInstance();
calendar2.setTime(time);
calendar2.add(Calendar.MINUTE,30);
System.out.println(time1.format(calendar2.getTime()));
calendar3 = Calendar.getInstance();
calendar3.setTime(time);
calendar3.add(Calendar.MINUTE,-30);
System.out.println(time1.format(calendar3.getTime()));

calendar4=Calendar.getInstance();

System.out.println(time1.format(calendar4.getTime()));
Date now = calendar4.getTime();
if (now.after(calendar3.getTime()) && now.before(calendar2.getTime())) {
//checkes whether the current time is between 14:49:00 and 20:11:13.
System.out.println(true);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

hh:mm a格式中YEAR字段是set to 1970 by default .

Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.

因此,在您的情况下,您希望在比较日期时忽略 YEARMONTHDAY_OF_MONTH 字段,所以这样做(这会重置 YEARMONTHDAY_OF_MONTH 字段):

calendar4.setTime(time1.parse(time1.format(calendar4.getTime())));

之后:

calendar4=Calendar.getInstance();

关于java - 比较时间戳时,年份转到 1970,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33345192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com