-4

I need to get today’s date at midnight in my time zone in the format YYYYMMDDHHMMss.000Z as date and time in GMT. I'm interested in the date, not the time of day. In my system when I have got a date: 20201001220000.000Z it is a day 20201003 as summertime for GMT. I am asking this question because I need to compare the 20201001220000.000Z with today’s date.

I need to get the date ( TODAY midnight) in format yyyyMMddHHmmss.SSSZ. TODAY midnight 20201004 in my system is: 20201003220000.000Z this date will be compared with other dates e.g 20201002220000.000Z. The problem is that I can't get midnight.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
PiotrP
  • 1
  • 1
  • 2
  • 2
    are you looking for [this](https://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java)? also, what do you mean by "*summer time for GMT*"? – FObersteiner Oct 03 '20 at 14:26
  • `String pattern = "YYYYMMDDHHMM.SSSZ"; // YYYYMMDDHHMMss.000Z SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat.format(new Date()); System.out.println(date);` – Mauricio Kenny Oct 03 '20 at 14:36
  • What did your search bring up? Similar questions have been asked and answered a thousand times (not sure I’m exaggerating), how could you miss them? – Ole V.V. Oct 03 '20 at 14:40
  • @MauricioKenny Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. (Also your pattern uses wrong case of some of the letters.) – Ole V.V. Oct 03 '20 at 14:41
  • 1
    It’s not really clear, sorry. Have you got a string like `20201001220000.000Z`, and you want to convert it to date only in your own time zone like `20201003`, taking summer time in your time zone into account? – Ole V.V. Oct 03 '20 at 14:47
  • Does this answer your question? [How to convert UTC DateTime to another Time Zone using Java 8 library?](https://stackoverflow.com/questions/54108388/how-to-convert-utc-datetime-to-another-time-zone-using-java-8-library) – Ole V.V. Oct 03 '20 at 14:47
  • Also when the string you have got says October 1 (in GMT?), are your sure it’s October 3 in your time zone because of summer time (DST)? Which time zone are you in? – Ole V.V. Oct 03 '20 at 14:59
  • “20201001220000.000Z it is a day 20201003 as summer time for GMT.” No it is not. The ‘Z’ means UTC, which is (almost) the same as GMT. So 20201001220000.000Z is 20201001 GMT. If you want the date portion of that string, use `substring` to obtain the first eight characters. – VGR Oct 03 '20 at 15:02
  • 1
    This will give you a good picture of java.time: https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime – DigitShifter Oct 03 '20 at 15:53
  • @MauricioKenny - In addition to the valuable suggestion by Ole V.V., your format is also wrong. It should be `yyyyMMddHHmmss.SSSZ`, not `YYYYMMDDHHMM.SSSZ`. I hope, you understand the difference between `DD` and `dd`; if not, check https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Arvind Kumar Avinash Oct 03 '20 at 16:11
  • 1
    @ArvindKumarAvinash I understand the difference. I make a mistake. – Mauricio Kenny Oct 03 '20 at 16:22
  • Thank you for all hints: Sorry if I wasn't specific, I need to get the date ( TODAY midnight) in format yyyyMMddHHmmss.SSSZ . TODAY midnight 20201004 in my system is: 20201003220000.000Z this date will be comparison with other dates e.g 20201002220000.000Z . The problem is that I cant get midnight . – PiotrP Oct 04 '20 at 08:57

3 Answers3

3

java.time

Edit:

I tend to understand that you want to format today’s date into your format for comparison with other date-time strings in UTC (GMT) in the same format. For comparing dates and times I suggest that you compare date-time objects, not strings. So the options are two:

  1. Parse the existing string, convert it to a date in your time zone and compare it to today’s date.
  2. Parse your existing string into a point in time. Compare to the start of today’s date.

Let’s see both options in code.

1. Convert to date and compare dates:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmss.SSSX");
    ZoneId zone = ZoneId.systemDefault();
    
    LocalDate today = LocalDate.now(zone);
    
    String gmtString = "20201001220000.000Z";
    LocalDate dateFromGmtString = formatter.parse(gmtString, Instant::from)
            .atZone(zone)
            .toLocalDate();
    
    if (dateFromGmtString.isAfter(today)) {
        System.out.println(gmtString + " is in the future");
    } else if (dateFromGmtString.isBefore(today)) {
        System.out.println(gmtString + " was on a past date");
    } else {
        System.out.println(gmtString + " is today");
    }

Output:

20201001220000.000Z was on a past date

2. Find start of today’s date and compare times

    Instant startOfDay = LocalDate.now(zone).atStartOfDay(zone).toInstant();
    
    String gmtString = "20201001220000.000Z";
    Instant timeFromGmtString = formatter.parse(gmtString, Instant::from);
    
    if (timeFromGmtString.isBefore(startOfDay)) {
        System.out.println(gmtString + " was on a past date");
    } else {
        System.out.println(gmtString + " is today or later");
    }

20201001220000.000Z was on a past date

Original answer

You may be after the following. I recommend that you use java.time, the modern Java date and time API, for your date and time work.

DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmss.SSSX");

String gmtString = "20201001220000.000Z";

Instant instant = sourceFormatter.parse(gmtString, Instant::from);
LocalDate date = instant.atZone(ZoneId.systemDefault()).toLocalDate();
String dayString = date.format(DateTimeFormatter.BASIC_ISO_DATE);

System.out.println(dayString);

When I run the code in Europe/Warsaw time zone, the output is:

20201002

So the date has been converted from October 1 GMT to October 2 in Poland.

Edit:

… How can I get midnight?

To get the start of the day (usually 00:00):

    ZonedDateTime startOfDay = time.atZone(ZoneId.systemDefault())
            .truncatedTo(ChronoUnit.DAYS);
    System.out.println(startOfDay);

2020-10-02T00:00+02:00[Europe/Warsaw]

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Try this.

String format = "yyyy-MM-dd HH:mm:ss.SSS O";
        
ZonedDateTime zdt = ZonedDateTime.now().withZoneSameInstant(ZoneId.of("GMT"));
System.out.println(zdt.format(DateTimeFormatter.ofPattern(format)));

Prints a US East coast time as

2020-10-03 14:47:18.809 GMT

You can delete the spaces, dashes and colons as desired. But do not use Date or related formatters as they are outdated. Use the classes from the java.time package.

WJS
  • 36,363
  • 4
  • 24
  • 39
0

You can do it as follows:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Today
        LocalDate today = LocalDate.now();

        // Midnight in the JVM's default time-zone
        ZonedDateTime zdt = today.atStartOfDay(ZoneId.systemDefault());

        // Printing zdt in its default format i.e. value returned by zdt.toString()
        System.out.println(zdt);

        // Printing zdt in your custom format
        DateTimeFormatter customFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSSZ");
        String dateTimeStrCustom = zdt.format(customFormat);
        System.out.println(dateTimeStrCustom);
    }
}

Output:

2020-10-04T00:00+01:00[Europe/London]
20201004000000.000+0100
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110