在Java中操作日期和时间戳的最好方法是什么? [英] What's the best way to manipulate Dates and Timestamps in Java?

查看:114
本文介绍了在Java中操作日期和时间戳的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我需要在Java中使用日期和/或时间戳时,我总是觉得我做错了事情,花费无数时间尝试找到更好的方法来处理API,而无需编写自己的日期和时间效用类。这是一个令人讨厌的事情,我遇到了:

Every time I need to work with date and/or timstamps in Java I always feel like I'm doing something wrong and spend endless hours trying to find a better way of working with the APIs without having to code my own Date and Time utility classes. Here's a couple of annoying things I just ran into:


  • 0个月。我意识到最好的做法是使用Calendar.SEPTEMBER而不是8,但令人讨厌的是8代表9月而不是8月。

  • 0-based months. I realize that best practice is to use Calendar.SEPTEMBER instead of 8, but it's annoying that 8 represents September and not August.

获取没有时间戳的日期。我总是需要这个实用程序,Zeros将日期的时间戳部分删除。

Getting a date without a timestamp. I always need the utility that Zeros out the timestamp portion of the date.

我知道过去还有其他问题,但是不能召回。您可以随时在回复中添加更多信息。

I know there's other issues I've had in the past, but can't recall. Feel free to add more in your responses.

所以,我的问题是...你的第三方API用来简化Java对Date和Time操作的使用(如有)?关于使用 Joda 的任何想法? JSR-310日期和时间API有什么看法吗?

So, my question is ... What third party APIs do you use to simplify Java's usage of Date and Time manipulation, if any? Any thoughts on using Joda? Anyone looked closer at JSR-310 Date and Time API?

推荐答案

这篇文章对比较Java日期/时间API与JODA有很好的讨论。

This post has a good discussion on comparing the Java Date/Time API vs JODA.

我个人只是使​​用公历日历 SimpleDateFormat 任何时候我需要在Java中处理日期/时间。我从来没有在使用Java API时遇到任何问题,发现它很容易使用,所以没有真正考虑任何替代方案。

I personally just use Gregorian Calendar and SimpleDateFormat any time I need to manipulate dates/times in Java. I've never really had any problems in using the Java API and find it quite easy to use, so have not really looked into any alternatives.

这篇关于在Java中操作日期和时间戳的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆