0
public class ParseDate {
    public static final String DATE_FORMAT        = "yyyy-MM-dd";
    public static SimpleDateFormat DateFormatter = new SimpleDateFormat(DATE_FORMAT);
    public static void main(String[] args) {
        
        try {
            System.out.println("Converting 2020-12-31 to "+DateFormatter.parse("2020-12-31"));
            System.out.println("Converting 2020-06-30 to "+DateFormatter.parse("2020-06-30"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

Output:

Converting 2020-12-31 to Thu Dec 31 00:00:00 GMT 2020
Converting 2020-06-30 to Tue Jun 30 00:00:00 BST 2020

If I execute this code, I am getting different time zones (GMT and BST) as output. How to get same time zone of Date object as output irrespective of different input strings.

When I was executed the code, I expect the date object should contain same time zone (either GMT or BST). I have tried with the below time zone: Windows time zone tried

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Nag
  • 3
  • 1
  • I strongly recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Just `LocalDate.parse("2020-12-31")` gives you the `LocalDate` object that you want and need. – Ole V.V. Dec 02 '22 at 21:57
  • You are being fooled by the confusing `Date` class. It does not contain any time zone at all! You seem to be in (or have your JVM set to) a time zone that uses Greenwich Mean Time during standard time and British Summer Time during summer time (DST), which is part of the explanation of your different output. – Ole V.V. Dec 02 '22 at 21:59
  • Does this answer your question? [Java Date timezone printing different timezones for different years, Workaround needed](https://stackoverflow.com/questions/40815343/java-date-timezone-printing-different-timezones-for-different-years-workaround). [The answer by Basil Bourque](https://stackoverflow.com/a/41071394/5772882) uses java.time as I recommend. – Ole V.V. Dec 02 '22 at 22:12

1 Answers1

1

java.util.Date is not a true date-time object; rather, it just represents the number of milliseconds from January 1, 1970, 00:00:00 GMT. The Date#toString returns this millisecond value into a string applying the default timezone which is Europe/London in your case and therefore it prints GMT and BST because of DST.

java.time

The java.time API, released with Java-8 in March 2014, supplanted the error-prone legacy date-time API. Since then, using this modern date-time API has been strongly recommended.

Demo using modern date-time API

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

public class Main {
    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.of("Europe/London");
        ZonedDateTime zdt1 = LocalDate.parse("2020-12-31").atStartOfDay(zoneId);
        ZonedDateTime zdt2 = LocalDate.parse("2020-06-30").atStartOfDay(zoneId);
        System.out.println("Converting 2020-12-31 to " + zdt1);
        System.out.println("Converting 2020-06-30 to " + zdt2);

        // Date#toString like format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
        System.out.println(zdt1.format(formatter));
        System.out.println(zdt2.format(formatter));
    }
}

Output:

Converting 2020-12-31 to 2020-12-31T00:00Z[Europe/London]
Converting 2020-06-30 to 2020-06-30T00:00+01:00[Europe/London]
Thu Dec 31 00:00:00 GMT 2020
Tue Jun 30 00:00:00 BST 2020

Note that since java.time API is based on ISO 8601 standard, you do not need to specify a parser ( DateTimeFormatter in the case of java.time API) to parse a date string which is already in this format.

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110