简体   繁体   English

如何将两个不同的字符串转换为日期格式并在JSP / JAVA中找到它们的差异

[英]How to convert two different strings to date format and finding their differences in JSP/JAVA

I have 2 strings Str1: 2016-7-25.15.38. 我有2个字符串Str1:2016-7-25.15.38。 32. 0 (This is what im getting from DB) String2 : July 25, 2016 3:19 PM (This one I wrote a program to read from the email timestamp) 32. 0(这是我从数据库中获得的信息)String2:2016年7月25日,3:19 PM(我编写了一个程序来从电子邮件时间戳中读取内容)

How to convert these 2 strings to date format and find their differnce in time.. Pls help. 如何将这两个字符串转换为日期格式并找到它们在时间上的差异。 Ive gone through so many pages in SO and google but not getting anything specific 我已经在SO和Google中浏览了很多页面,但没有得到任何具体的信息

How to convert these 2 strings to date format 如何将这两个字符串转换为日期格式

Using Java 8, you'd parse them like this: 使用Java 8,您可以这样解析它们:

ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime dt1 = ZonedDateTime.parse("2016-7-25.15.38. 32. 0",
            DateTimeFormatter.ofPattern("uuuu-M-d.H.m. s. 0").withZone(zoneId));
ZonedDateTime dt2 = ZonedDateTime.parse("July 25, 2016 3:19 PM",
            DateTimeFormatter.ofPattern("MMMM d, uuuu h:mm a").withZone(zoneId));

Using a non-standard indentation to align format string with value to be parsed, for easier comparison. 使用非标准缩进将格式字符串与要解析的值对齐,以便于比较。

The code is using ZonedDateTime so Daylight Savings Time will be handled correctly. 代码使用的是ZonedDateTime因此可以正确处理夏令时。


and find their difference in time 并发现他们的时间差异

Getting the difference in time is then easy enough, using until() or between() : 然后,使用until()between()很容易地获得时间差:

long seconds = dt1.until(dt2, ChronoUnit.SECONDS);
long seconds = ChronoUnit.SECONDS.between(dt1, dt2);

They are the same. 他们是一样的。 Use whichever you like best. 使用最喜欢的一个。


If you print the values above, you get (I'm in USA Eastern time zone): 如果您打印上述值,您将获得(我在美国东部时区):

2016-07-25T15:38:32-04:00[America/New_York]
2016-07-25T15:19-04:00[America/New_York]
-1172

I would simply use Oracle to figure out the difference. 我只是使用Oracle来找出差异。 It's quite easy: 这很容易:

select to_date('2016-07-25.15.38.32', 'yyyy-mm-dd.hh24.mi.ss') - to_date('July 25, 2016 3:19 PM', 'Month dd, yyyy hh:mi PM') from dual 选择to_date('2016-07-25.15.38.32','yyyy-mm-dd.hh24.mi.ss')-to_date('2016年7月25日3:19 PM','dd dd,yyyy hh:mi PM ')来自双重

First you need to parse those two strings into Date objects. 首先,您需要将这两个字符串解析为Date对象。 You can use SimpleDateFormat class for parsing. 您可以使用SimpleDateFormat类进行解析。 Once you have two Data objects, you can easily get the date difference. 一旦有了两个Data对象,就可以轻松获得日期差。

1. Convert java.sql.timestamp into java.util.Date 1. 将java.sql.timestamp转换为java.util.Date

Date startDate = new Date(startTimestamp.getTime());
Date endDate= new Date(endTimestamp.getTime());

2.You can find the time difference using TimeUnit enum as below. 2.您可以使用TimeUnit枚举找到时差 ,如下所示。

Date startDate = // Set start date
Date endDate   = // Set end date

long duration  = endDate.getTime() - startDate.getTime();

long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);

and so on.. 等等..

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

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