将 String 日期转换为 Java Date 对象

发布于 2024-11-29 10:54:25 字数 441 浏览 2 评论 0原文

可能的重复:
计算 Java 中的日期差异
如何计算时间Java中的span并格式化输出?

假设您获得了两个日期作为字符串,并且它们采用这种格式 8/11/11 9:16:36 PM 我将如何进行转换它们到 java Date 对象,这样我那么可以计算两个日期之间的差异吗?

Possible Duplicates:
Calculating difference in dates in Java
How can I calculate a time span in Java and format the output?

Say you were given two dates as strings and they are in this format 8/11/11 9:16:36 PM how would I go about converting them to java Date objects so that I can then calculate the difference between two dates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏尉蓝 2024-12-06 10:54:25

只要两个时间都是 GMT/UTC,您就可以执行 date1.getTime() - date2.getTime(),然后将结果除以 86400000 以获得天数。其余的可能非常直观。

编辑 - 基于编辑的问题

要将字符串转换为日期,请使用 SimpleDateFormat 类。

String yourDateString = "8/11/11 9:16:36 PM";
SimpleDateFormat format =
            new SimpleDateFormat("MM/dd/yy HH:mm:ss a");
Date yourDate = format.parse(yourDateString);

As long as both times are in GMT/UTC, you can do date1.getTime() - date2.getTime() and then divide the result by 86400000 to get number of days. The rest is probably pretty intuitive.

EDIT -- based on edited question

To convert Strings into dates, use the SimpleDateFormat class.

String yourDateString = "8/11/11 9:16:36 PM";
SimpleDateFormat format =
            new SimpleDateFormat("MM/dd/yy HH:mm:ss a");
Date yourDate = format.parse(yourDateString);
半寸时光 2024-12-06 10:54:25

大多数 Date 的 getter 已被弃用,取而代之的是 Calendar 方法。以下是您的做法

Date date1, date2; //initialized elsewhere
Calendar day1 = new Calendar();
day1.setTime(date1)

Calendar day2 = new Calendar();
day2.setTime(date2);


int yearDiff, monthDiff, dayDiff, hourDiff, minuteDiff, secondDiff;
yearDiff = Math.abs(day1.get(Calendar.YEAR)-day2.get(Calendar.YEAR));
monthDiff = Math.abs(day1.get(Calendar.MONTH)-day2.get(Calendar.MONTH));
dayDiff = Math.abs(day1.get(Calendar.DAY_OF_YEAR)-day2.get(Calendar.DAY_OF_YEAR));
hourDiff = Math.abs(day1.get(Calendar.HOUR_OF_DAY)-day2.get(Calendar.HOUR_OF_DAY));
minuteDiff = Math.abs(day1.get(Calendar.MINUTE)-day2.get(Calendar.MINUTE));
secondDiff = Math.abs(day1.get(Calendar.SECOND)-day2.get(Calendar.SECOND));

然后您可以对这些数字做任何您喜欢的事情。

The majority of Date's getters are deprecated, replaced with Calendar methods. Here's how you would do it

Date date1, date2; //initialized elsewhere
Calendar day1 = new Calendar();
day1.setTime(date1)

Calendar day2 = new Calendar();
day2.setTime(date2);


int yearDiff, monthDiff, dayDiff, hourDiff, minuteDiff, secondDiff;
yearDiff = Math.abs(day1.get(Calendar.YEAR)-day2.get(Calendar.YEAR));
monthDiff = Math.abs(day1.get(Calendar.MONTH)-day2.get(Calendar.MONTH));
dayDiff = Math.abs(day1.get(Calendar.DAY_OF_YEAR)-day2.get(Calendar.DAY_OF_YEAR));
hourDiff = Math.abs(day1.get(Calendar.HOUR_OF_DAY)-day2.get(Calendar.HOUR_OF_DAY));
minuteDiff = Math.abs(day1.get(Calendar.MINUTE)-day2.get(Calendar.MINUTE));
secondDiff = Math.abs(day1.get(Calendar.SECOND)-day2.get(Calendar.SECOND));

Then you can do whatever you like with those numbers.

转角预定愛 2024-12-06 10:54:25

定义一个 SimpleDateFormat 匹配您的格式( java 文档非常简单),然后使用 parse 方法获取正确的 Date 对象,从中您可以轻松计算两个日期之间的差异。
一旦有了这种差异,最好的方法可能是“手动”计算天数/小时/分钟/秒数,尽管可以再次使用 SimpleDateFormat (或其他一些格式化机制)来显示正确的值通用方式。

define a SimpleDateFormat matching your format (the java doc is pretty straighforward), then use the parse method to get a the proper Date object, from which you can easily compute the difference between the two dates.
Once you have this difference, the best is probably to compute "manually" the number of days / hours / minutes / seconds, although it might be possible to again use a SimpleDateFormat (or some other formatting mechanism) to display the proper values in a generic way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文