-1

I am writting a java application and I just started with 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] . 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?

dres
  • 499
  • 6
  • 18
  • 1
    Parse the format to a `java.util.Date` and use the `compareTo()` or `before()` and `after()` methods. Or put the dates into a query etc., depending on how `I want to take only the objects that have time stamp between the two dates` would/should be implemented (hint: details needed). – Thomas Sep 23 '15 at 07:47
  • Which Java version? Are you using Java 8? (please say yes) ... If so, please have a look at the new Java 8 DateTime API: http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html and https://docs.oracle.com/javase/tutorial/datetime/ – Dominik Sandjaja Sep 23 '15 at 07:47
  • @DaDaDom I am using java 6 – dres Sep 23 '15 at 07:49
  • http://stackoverflow.com/questions/16785643/get-the-list-of-dates-between-two-dates – Manan Patel Sep 23 '15 at 07:57
  • @dres Please post additional information in the Question rather than comments. – Basil Bourque Sep 23 '15 at 20:13

4 Answers4

1
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

1

Java 8

Start by converting the String value to something which is comparable...

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. 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 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)

Now, you should be able to use something similar with using Joda-Time

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I so want to convert all of our legacy software to Java 8 ... big part of the reasoning is the new date api. – Thomas Sep 23 '15 at 08:02
  • @Thomas I died for `try-with-resources` when Java 7 came out :P - Know your pain ;) – MadProgrammer Sep 23 '15 at 08:03
  • Really? This deserved a downvote because? If I've done something wrong, I'd love to learn what so I can correct it and learn from it – MadProgrammer Sep 23 '15 at 09:13
  • Maybe it's because the OP said he's using Java 6 and this is on Java 8. If I had another vote, I'd upvote it twice :) - But don't mind it, I also received a downvote, also without explanation. – Thomas Sep 23 '15 at 09:15
  • @Thomas Had the op mentioned java 6 in the original question, I would have done it using JodaTime instead :P but I'd already posted the answer before java 6 was mentioned. The version if java shouldn't effect the voting, as another user with a similar problem using java 8 would find it useful (I hope) ;) but thanks anyway – MadProgrammer Sep 23 '15 at 09:18
0

To parse the date use 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.

Alternatively use a sorted map with the objects' date as key.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

Create your from/to ZonedDateTime's as MadProgrammer said:

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

Then create Joda DateTime's for from/to:-

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.

jacks
  • 4,614
  • 24
  • 34