简体   繁体   English

Java 给出不同日期的日期

[英]Java Date giving different dates

I need my Java Android app to extract and compare dates, but it's all a mess to get the correct date.我需要我的 Java Android 应用程序来提取和比较日期,但是要获得正确的日期是一团糟。

    TimeZone timeZone = TimeZone.getTimeZone("GMT");
    Calendar calendar = Calendar.getInstance(timeZone);

    Date currentTime = calendar.getTime();
    Log.i(logtag, "Date currentTime="+currentTime);

    String tidspunkt = calendar.get(Calendar.YEAR)+ "." + calendar.get(Calendar.MONTH)+"."+calendar.get(Calendar.DAY_OF_MONTH)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
    Log.i(logtag, "calendar.get(Calendar.YEAR)="+tidspunkt);

    String tidspunkt2 = currentTime.getYear()+ "." + currentTime.getMonth()+"."+currentTime.getDay()+" "+currentTime.getHours()+":"+currentTime.getMinutes()+":"+currentTime.getSeconds();
    Log.i(logtag, "currentTime.getYear()="+tidspunkt2);

    currentTime.setYear(calendar.get(Calendar.YEAR));
    currentTime.setMonth((calendar.get(Calendar.MONTH)));
    currentTime.setDate(calendar.get(Calendar.DAY_OF_MONTH));
    currentTime.setHours(calendar.get(Calendar.HOUR_OF_DAY));
    currentTime.setMinutes(calendar.get(Calendar.MINUTE));
    currentTime.setSeconds(calendar.get(Calendar.SECOND));
    Log.i(logtag, "Date currentTime edited="+currentTime);

    String tidspunkt3 = currentTime.getYear()+ "." + currentTime.getMonth()+"."+currentTime.getDay()+" "+currentTime.getHours()+":"+currentTime.getMinutes()+":"+currentTime.getSeconds();
    Log.i(logtag, "currentTime.getYear() edited="+tidspunkt3);

Actual time when running: Mon 10th of January 2022, 22:29实际运行时间:2022年1月10日星期一22:29

Results:结果:

  • Date currentTime=Mon Jan 10 22:29:53 GMT+01:00 2022 (correct date)日期 currentTime=Mon Jan 10 22:29:53 GMT+01:00 2022(正确日期)
  • calendar.get(Calendar.YEAR)=2022.0.10 21:29:53 (wrong month, wrong hour) calendar.get(Calendar.YEAR)=2022.0.10 21:29:53(错误的月份,错误的时间)
  • currentTime.getYear()=122.0.1 22:29:53 (wrong year, wrong month, wrong day) currentTime.getYear()=122.0.1 22:29:53(年错月错日)
  • Date currentTime edited=Tue Jan 10 21:29:53 GMT+01:00 3922 (wrong year, wrong weekday, wrong hour)日期当前时间编辑=1 月 10 日星期二 21:29:53 GMT+01:00 3922(错误的年份、错误的工作日、错误的时间)
  • currentTime.getYear() edited=2022.0.2 21:29:53 (wrong month, wrong day, wrong hour) currentTime.getYear() 编辑=2022.0.2 21:29:53 (错误的月份,错误的日期,错误的时间)

Why do I get different years, months, days, week days and hours when they all should've been the same?为什么我得到不同的年份、月份、日期、工作日和小时,而它们本应相同? I need to save old dates as objects, and then be able to pull the correct year, month number, day number etc from those objects.我需要将旧日期保存为对象,然后能够从这些对象中提取正确的年、月数、日数等。 I shouldn't need to make my own date objects...我不应该需要制作自己的约会对象......

This is why java.util.Date and java.util.Calendar are terrible APIs that you should replace with java.time .这就是为什么java.util.Datejava.util.Calendar是糟糕的 API,应该用java.time替换。 Have you read the doc on Date.setYear ?您是否阅读过Date.setYear上的文档? Or seen that Calendar.JANUARY is 0?或者看到Calendar.JANUARY是 0?

You can use java.time with Android with desugaring .您可以将java.timeAndroid与脱糖一起使用。

Avoid legacy classes避免遗留类

Never use TimeZone, Calendar, Date.永远不要使用 TimeZone、Calendar、Date。 Do not waste your time trying to understand their bizarre behavior.不要浪费时间试图了解他们的奇怪行为。 Sun, Oracle, and the JCP community gave up on those classes years ago, and do should you. Sun,Oracle 和 JCP 社区几年前就放弃了这些课程,你应该这样做。 These terrible classes were years ago supplanted by the modern java.time classes defined in JSR 310.这些可怕的类在几年前被 JSR 310 中定义的现代java.time类所取代。

java.time java.time

Capture the current moment as seen in UTC (an offset of zero).捕获以 UTC 显示的当前时刻(偏移量为零)。

Instant instant = Instant.now() ;

The Instant class is a basic building block class with limited features. Instant class 是功能有限的基本构建块 class。 For more flexibility, convert to either OffsetDateTime or ZonedDateTime .为了获得更大的灵活性,请转换为OffsetDateTimeZonedDateTime

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
ZonedDateTime zdt = instant.atZone( ZoneId.of( "Asia/Tokyo" ) ) ;

Note that all three of these objects here, instant , odt , & zdt , all represent the very same simultaneous moment, the same single point on the time line.请注意,此处的所有这三个对象, instantodtzdt ,都代表相同的同时时刻,时间线上的相同单点。

To generate text representing a moment, you are working much too hard.要生成代表某个时刻的文本,您的工作量太大了。 Let DateTimeFormatter do the heavy lifting.DateTimeFormatter完成繁重的工作。

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG ).withLocale( locale ) ;
String output = zdt.format( f ) ;

An implementation of java.time is built into Android 26+. java.time的实现内置在 Android 26+ 中。 For earlier Android, the latest tooling provides most of the java.time functionality via “API desugaring”.对于早期的 Android,最新的工具通过“API 脱糖”提供了大部分java.time功能。 If that does not work for you, use a back-port of java.time , the ThreeTenABP project.如果这对您不起作用,请使用ThreeTenABP项目java.time的后向端口。

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

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