1

How can I covert a given date to a UTC start date time and end date time.

String date = "2017-01-01"
//Needed startDate as 2017-01-01T00:00:00Z
//and end date as 2017-01-01T23:59:59Z
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
user2358262
  • 257
  • 3
  • 13

2 Answers2

1

It depends on your java version and as always there are many ways to solve this. With java 8 in place you can do something like this:

First you need to parse the date into a java.time.LocalDate

String date = "2017-01-01";
final LocalDate localDate = LocalDate.parse(date);
System.out.println(localDate);

Which prints

2017-01-01

From this LocalDate you can create your UTC start and end dates as types of java.time.ZonedDateTime as follows

final ZoneId utc = ZoneId.of("UTC");
final ZonedDateTime start = LocalDate.parse(date).atStartOfDay(utc);
final ZonedDateTime end = LocalDate.parse(date).atTime(23, 59, 59).atZone(utc);
System.out.println(end); 

This prints

2017-01-01T00:00Z[UTC]
2017-01-01T23:59:59Z[UTC]

Notice for the end date you could also do

final ZoneId utc = ZoneId.of("UTC");
final ZonedDateTime end = LocalDate.parse(date).atTime(LocalTime.MAX).atZone(utc);

Which gives you a few more digits

2017-01-01T23:59:59.999999999Z[UTC]

See also

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html

Hope it helps!

Daniel Bubenheim
  • 4,043
  • 3
  • 14
  • 20
  • (a) For UTC, use the constant `ZoneOffset.UTC`. (b) When using an offset-from-UTC rather than a time zone, use the `OffsetDateTime` class rather than `ZonedDateTime`. `OffsetDateTime odt = localDate.atTime( LocalTime.MIN ) ;` – Basil Bourque Jan 19 '17 at 21:20
  • yes that's true. As I said there are of course many ways to achieve results. I think we gave at least some small examples. Now it's up to the the author how to proceed. – Daniel Bubenheim Jan 19 '17 at 21:28
  • Here is also a nice explanation on this topic http://stackoverflow.com/a/39507023/6372139 – Daniel Bubenheim Jan 19 '17 at 21:29
  • Thanks this worked for me. I have one question, why do `LocalDate.parse(date).atTime(0, 0, 0).atZone(utc);` truncate seconds from the datetime? – user2358262 Jan 20 '17 at 19:05
0

The question is actually answered using Java 8 feature. But if you're like me and sitting on the older versions of Java, the following might serve the purpose even though it is not as elegant as the other solution:

    String dateString = "2017-01-01"; 
    final SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
    // we need Calendar to manipulate date components
    Calendar cal = new GregorianCalendar();
    cal.setTime(sdfInput.parse(dateString));
    
    final SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    String startDate = sdfOutput.format(cal.getTime());
    
    cal.set(Calendar.HOUR, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    String endDateString = sdfOutput.format(cal.getTime());
    
    System.out.println(startDate);
    System.out.println(endDateString);

The output:

2017-01-01T00:00:00Z

2017-01-01T23:59:59Z

Community
  • 1
  • 1
ujulu
  • 3,289
  • 2
  • 11
  • 14