简体   繁体   English

Java日期格式获取两个日期值之间的所有日期

[英]Java date format get all dates between two date values

I am writting a java application and I just started with java Date . 我正在编写一个Java应用程序,而我刚开始使用java Date What I am trying to do is to read a json that contains two date stamps that look like this: 2015-05-20T17:24Z[UTC] . 我正在尝试做的是读取一个包含两个日期戳的json2015-05-20T17:24Z[UTC] After I read the two dates I want to take only the objects that have time stamp between the two dates I have just read. 读完两个日期后,我只想拍摄在刚读过的两个日期之间带有时间戳的对象。 Can anyone help me on how to work with this format? 谁能帮我使用这种格式吗?

public static List<Date> getDaysBetweenDates(Date startdate, Date enddate)
{
    List<Date> dates = new ArrayList<Date>();
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(startdate);

    while (calendar.getTime().before(enddate))
    {
        Date result = calendar.getTime();
        dates.add(result);
        calendar.add(Calendar.DATE, 1);
    }
    return dates;
   }

Function above will list all the valid date objects between two dates 上面的函数将列出两个日期之间的所有有效日期对象

Java 8 Java 8

Start by converting the String value to something which is comparable... 首先将String值转换为可比较的值...

String text = "2015-05-20T17:24Z[UTC]";
ZonedDateTime from = ZonedDateTime.parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME);

Now, (obviously), you need a to date as well, but the conversion is the same process. 现在,(显然),你需要一个to日期为好,但转换是相同的过程。 When you need to, convert the value you want to compare to a ZonedDateTime object (as above) and use it's functionality to determine if it's within the specified range... 必要时,将要比较的值转换为ZonedDateTime对象(如上所述),并使用其功能来确定其是否在指定范围内...

ZonedDateTime from = ...;
ZonedDateTime to = ...;
ZonedDateTime date = ...;

if (date.isAfter(from) && date.isBefore(to)) {

}

Now, this is exclusive, if you want the from and to dates to be inclusive, you'll need to add a isEqual check for both the from and to dates (but it only needs to match one, obviously) 现在,这是唯一的,如果你想fromto日期是包容性的,你需要添加isEqual同时检查了fromto日期(但只需要匹配一个,很明显)

Now, you should be able to use something similar with using Joda-Time 现在,您应该可以使用与Joda-Time类似的功能

To parse the date use SimpleDateFormat : 要解析日期,请使用SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm'Z'[ZZZ]" );
Date date = sdf.parse( "2015-05-20T17:24Z[UTC]" );

Then either loop over the objects you want to filter and check object.date.compareTo(startDate) >= 0 and object.date.compareTo(endDate) <= 0 etc. 然后循环遍历要过滤的对象并检查object.date.compareTo(startDate) >= 0object.date.compareTo(endDate) <= 0等。

Alternatively use a sorted map with the objects' date as key. 或者,使用以对象的日期为键的排序地图。

Create your from/to ZonedDateTime's as MadProgrammer said: 如MadProgrammer所说,从/到ZonedDateTime创建您的:

ZonedDateTime from = ZonedDateTime.parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME);

Then create Joda DateTime 's for from/to:- 然后从/到创建Joda DateTime :-

DateTimeZone zone = DateTimeZone.forID(zdt.getZone().getId());
DateTime from = new DateTime(zdt.toInstant().toEpochMilli());

Then use Joda's Interval.contains() to check if each Instant falls within the interval - keeping in mind that contains() excludes the end date. 然后使用Joda的Interval.contains()检查每个Instant是否落在该间隔内-请记住, contains()不包括结束日期。

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

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