0

Here I am trying to parse two dates in java. Date 1 is "2021-01-01" and date 2 is "2021-04-01". But after parsing Java generating Date object with a different timezone. Really confused by this behavior. I am looking at the same timezone and that is EDT.

import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    
    
    public class DateCalculation {
    
        private static final DateFormat formater = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    
        public static void main(String args[]) throws ParseException {
            Date date1 = getDateFromString("2021-01-01");
            System.out.println("Date 1: " + date1);
    
            Date date2 = getDateFromString("2021-04-01");
            System.out.println("Date 2: " + date2);
        }
    
        public static Date getDateFromString(String dateString) throws ParseException {
            if(dateString == null || dateString.trim().length() == 0)
                return null;
    
            dateString = dateString.replace("\"", ""); // Remove quotes
            return formater.parse(dateString);
        }
    }


Output:
Date 1: Fri Jan 01 00:00:00 EST 2021
Date 2: Thu Apr 01 00:00:00 EDT 2021
7
  • 2
    There is no EDT in January. Commented May 2, 2022 at 23:46
  • 2
    Also, remember, Date#toString is using an internal formatter, oh, and you should be using the java.time API over the older java.util based date classes Commented May 2, 2022 at 23:50
  • Can you share with me the code snippet, please? Commented May 3, 2022 at 0:15
  • “EDT” is not a real time zone. Commented May 3, 2022 at 0:18
  • 1
    I think I understand your confusion. And I don’t think it’s documented behaviour, but this is as confusing the old Date class was behaving (and SimpleDateFormat was even worse). I say ‘was’ because the good solution, really the only sensible solution is to stop using those classes and switch to java.time, the modern Java date and time API. Date and related classes are long outdated. Commented May 3, 2022 at 1:40

1 Answer 1

2

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

LocalDate

For a date-only value without time-of-day and without time zone, use LocalDate class.

LocalDate.parse( "2021-04-01" )

ZonedDateTime

If you want to represent the first moment of the day for that date in a particular time zone, apply ZoneId to get a ZonedDateTime.

Do not assume the day starts at 00:00:00 🕛. Some dates in some zones start at another time such as 01:00:00 🕐. So let java.time determine the first moment.

EDT is not a real time zone. Perhaps you meant America/Montreal, America/New_York, America/Indiana/Marengo, or some such.

LocalDate ld = LocalDate.parse( "2022-01-23" ) ;
ZoneId z = ZoneId.of( "America/Indiana/Marengo" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.