gpt4 book ai didi

java - 下午 4 点就下类了

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:22 26 4
gpt4 key购买 nike

我试图显示两个日期之间的差异,按月、日、小时、分钟和秒分割。下面的代码似乎可以工作,只是时间总是晚 4 点。我尝试了 HOUR 和 HOUR_OF_DAY,但得到了相同的结果。

    Calendar cal = Calendar.getInstance();

int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);


DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

try {
//get the current date/time
Date beginDate = cal.getTime();

//fabricate a future date/time
Date endDate = dateFormat.parse(String.valueOf(year) + "-" + String.valueOf(month)
+ "-" + String.valueOf(day+95) + " " + String.valueOf(hour+2)
+ ":" + String.valueOf(minutes +16) + ":" + String.valueOf(seconds +34));

long millis = endDate.getTime() - beginDate.getTime();

TextView txtMonths = (TextView) findViewById(R.id.months);
txtMonths.setText("Months: " + (new SimpleDateFormat("M")).format(new Date(millis)));

TextView txtDays = (TextView) findViewById(R.id.days);
txtDays.setText("Days: " + (new SimpleDateFormat("d")).format(new Date(millis)));

TextView txtHours = (TextView) findViewById(R.id.hours);
txtHours.setText("Hours: " + (new SimpleDateFormat("h")).format(new Date(millis)));

TextView txtMinutes = (TextView) findViewById(R.id.minutes);
txtMinutes.setText("Minutes: " + (new SimpleDateFormat("m")).format(new Date(millis)));

TextView txtSeconds = (TextView) findViewById(R.id.seconds);
txtSeconds.setText("Seconds: " + (new SimpleDateFormat("s")).format(new Date(millis)));

} catch (ParseException ex) {
Log.d("ParseException", ex.toString());

}

有人认为这是因为时区。我是 GMT +8 并且 DST = true 所以

 TimeZone timeZone = cal.getTimeZone();
timeZone.getOffset( System.currentTimeMillis()));

返回 7 小时而不是 4 小时。我是否缺少时区信息?

最佳答案

这是错误的我以为我有它,但我没有。

我认为这是时区,但我仍然不确定当我处于 GMT+8 且处于 DST 时它是如何得出 4 的。因此,虽然下面的代码似乎有效,但如果它在 7 或 8 点之前关闭,我会感觉更舒服。如果是这种情况,时区我会立即明白。

Java 可能很残酷。

    Calendar cal = Calendar.getInstance();
TimeZone UTC = TimeZone.getTimeZone("UTC");
cal.setTimeZone(UTC);

int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);


DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

try {
//get the current date/time
Date beginDate = cal.getTime();

//fabricate a future date/time

endDate = dateFormat.parse(String.valueOf(year) + "-" + String.valueOf(month)
+ "-" + String.valueOf(day+95) + " " + String.valueOf(hour+2)
+ ":" + String.valueOf(minutes +1) + ":" + String.valueOf(seconds +34));
dateFormat.setTimeZone(UTC);

long millis = endDate.getTime() - beginDate.getTime();

if (TimeZone.getDefault().inDaylightTime( new Date())) {
millis += 3600000;
}

TextView txtMonths = (TextView) findViewById(R.id.months);
txtMonths.setText("Months: " + (new SimpleDateFormat("M")).format(new Date(millis)));

TextView txtDays = (TextView) findViewById(R.id.days);
txtDays.setText("Days: " + (new SimpleDateFormat("d")).format(new Date(millis)));

TextView txtHours = (TextView) findViewById(R.id.hours);
txtHours.setText("Hours: " + (new SimpleDateFormat("h")).format(new Date(millis)));

TextView txtMinutes = (TextView) findViewById(R.id.minutes);
txtMinutes.setText("Minutes: " + (new SimpleDateFormat("m")).format(new Date(millis)));

TextView txtSeconds = (TextView) findViewById(R.id.seconds);
txtSeconds.setText("Seconds: " + (new SimpleDateFormat("s")).format(new Date(millis)));
} catch (ParseException ex) {
Log.d("ParseException", ex.toString());

}

关于java - 下午 4 点就下类了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884121/

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