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

3
  • 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. Just LocalDate.parse("2020-12-31") gives you the LocalDate object that you want and need. Commented Dec 2, 2022 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. Commented Dec 2, 2022 at 21:59
  • Does this answer your question? Java Date timezone printing different timezones for different years, Workaround needed. The answer by Basil Bourque uses java.time as I recommend. Commented Dec 2, 2022 at 22:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.