如何在Android中转换日期和时间

huangapple go评论82阅读模式
英文:

How to cast date and time in Android

问题

在我的应用程序中,我想从服务器获取一些数据,然后我应该转换日期和时间!
我从服务器收到的日期和时间的格式如下:end_date: "2020-04-08 13:11:14"

我想要从我的设备获取当前的日期和时间,并与上述日期(end_date)进行计算,如果这个时间在24小时之内,我应该显示例如15小时后,但如果这个时间超过24小时,我应该显示2天后

但我不知道如何做到这一点?
你能帮我发送代码或者发送其他教程给我吗?
我已经搜索过了,但是没有找到相关内容。

英文:

In my application i want get some data from server and i should cast date and time!<br>
I receive date and time with this format end_date: &quot;2020-04-08 13:11:14&quot; from server.<br>

I want get now date and time from my device and to calculate with above date (end_date), if this time under 24h i should show for example 15 hour later, but if this time more than 24h i should show 2 days later!<br>

But i don't know how can i it?<br>
Can you help me with send code or send to me other tutorials?!<br>
I searched that but I didn't find anything.

答案1

得分: 1

尝试使用 datetime 库,或者你可以查看这个 答案

另外,如果可以选择的话,将时间从/到服务器传递为UTC时间是一个好习惯,可以节省一些本地化和时区相关的问题。

英文:

Try using the datetime library, or you can see this answer.

Also if you can choose, it's a good habit to pass the times in UTC from/to the server, it'll save some localization and timezone troubles.

答案2

得分: 1

java.time

现代方法使用 java.time 类。

解析输入字符串为 LocalDateTime

要解析,将中间的空格替换为 T,以符合 ISO 8601 标准。

String input = "2020-04-08 13:11:14".replace(" ", "T");
LocalDateTime ldt = LocalDateTime.parse(input);

您的输入缺少任何时区或偏移量指示器。因此,我们不知道这是否意味着在日本东京是下午 1 点,在法国图卢兹是下午 1 点,还是在美国俄亥俄州托莱多是下午 1 点。因此,您不能可靠地将其与当前日期和时间进行比较。

如果您想要在您的时区假设此字符串是用来表示时间的话,可以分配一个时区以获得 ZonedDateTime

ZoneId z = ZoneId.of("Asia/Tokyo");
ZonedDateTime then = ldt.atZone(z);

在相同的时区捕获当前时刻。

ZonedDateTime now = ZonedDateTime.now(z);

计算 24 小时后的时间。

ZonedDateTime twentyFourHoursFuture = now.plusHours(24);

比较两个时间。

boolean within24Hours = then.isBefore(twentyFourHoursFuture);

使用 Duration 类确定经过的时间。

Duration duration = Duration.between(then, now);

如果您想要信任 JVM 的当前默认时区,请调用 ZoneId.systemDefault。请注意,这个默认时区可能会在应用程序执行期间被其他 Java 代码更改。

ZoneId z = ZoneId.systemDefault();

关于 java.time

java.time 框架已内置于 Java 8 及更高版本。这些类取代了麻烦的旧遗留日期时间类,如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是 JSR 310

Joda-Time 项目,现在处于维护模式,建议迁移到 java.time 类。

您可以直接与数据库交换 java.time 对象。使用符合 JDBC 4.2 或更高版本的 JDBC 驱动程序。不需要字符串,也不需要 java.sql.* 类。

在哪里获取 java.time 类?

ThreeTen-Extra* 项目通过额外的类扩展了 java.time。这个项目是未来可能的 java.time 增加的一个实验场所。您可能会在这里找到一些有用的类,如 IntervalYearWeekYearQuarter 等等。

英文:

java.time

The modern approach uses java.time classes.

如何在Android中转换日期和时间

Parse the input string as a LocalDateTime.

To parse, replace the SPACE in the middle with a T to comply with ISO 8601 standard.

String input = &quot;2020-04-08 13:11:14&quot;.replace( &quot; &quot; , &quot;T&quot; ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;

Your input lacks an indicator of any time zone or offset-from-UTC. So we do not know if this was meant to be 1 PM in Tokyo Japan, 1 PM in Toulouse France, or 1 PM in Toledo Ohio US. So you cannot reliably compare this to the current date and time.

If you want to presume this string was meant to tell time in your time zone, then assign a time zone to get a ZonedDateTime.

ZoneId z = ZoneId.of( &quot;Asia/Tokyo&quot; ) ;
ZonedDateTime then = ldt.atZone( z ) ;

Capture the current moment in the same zone.

ZonedDateTime now = ZonedDateTime.now( z ) ;

Calculate 24 hours later.

ZonedDateTime twentyFourHoursFuture = now.plusHours( 24 ) ;

Compare.

boolean within24Hours = then.isBefore( twentyFourHoursFuture ) ;

Determine elapsed time using the Duration class.

Duration duration = Duration.between( then , now ) ;

If you want to trust the JVM’s current default time zone, call ZoneId.systemDefault. Beware that this default can be changed by other Java code during execution of your app.

ZoneId z = ZoneId.systemDefault() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

huangapple
  • 本文由 发表于 2020年4月7日 16:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61076142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定