0

I am trying to pass startDate's time as 00:00:00 and endDate's time as 23:59:59 but during debugging startDate's time is Thu Aug 09 10:30:00 IST 2018 and endDate's time is Tue Aug 14 10:29:59 IST 2018.Where am I doing wrong?

SimpleDateFormat estFormat=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
estFormat.setTimeZone(TimeZone.getTimeZone("EST"));

Date startDate=estFormat.parse(sdate+" 00:00:00");
object.setStartDate(startDate);

Date endDate=estFormat.parse(edate+" 23:59:59"); 
object.setEndDate(endDate);

provided the sdate and edate are strings which have dates in MM/dd/yyyy format.

SOLUTION: USING JAVA-TIME API

    sdate=sdate.trim()+" 00:00:00";
    edate=edate.trim()+" 23:59:59";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
    LocalDateTime localdatetime = LocalDateTime.parse(sdate,df);
    Date startDate = Date.from(localdatetime.atZone(ZoneId.of("America/New_York" )).toInstant());
    object.setStartDate(startDate);

    localdatetime = LocalDateTime.parse(edate,df);
    Date endDate = Date.from(localdatetime.atZone(ZoneId.of("America/New_York" )).toInstant());
    object.setEndDate(endDate);
Aakriti Gupta
  • 802
  • 1
  • 7
  • 16
  • 2
    Nothing is wrong, you got the US Eastern Time you requested. What you're seeing is those time converted to India Time when you *print* the values. It's a formatting issue on output. The values in memory are correct. Remember, a `java.util.Date` doesn't have a time zone, and is internally stored in UTC. Time zone conversions are applied when parsing and formatting the value. – Andreas Oct 16 '18 at 04:58
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 16 '18 at 05:00
  • so it means that the date passed in the code is correct? and will produce correct output?@Andreas – Aakriti Gupta Oct 16 '18 at 05:00
  • 1
    Ok I will try joda time and will post here if i face any error..@OleV.V. – Aakriti Gupta Oct 16 '18 at 05:02
  • Possible duplicate of [Calendar returns date in wrong time zone](https://stackoverflow.com/questions/26678030/calendar-returns-date-in-wrong-time-zone) – Ole V.V. Oct 16 '18 at 05:03
  • 1
    Just to clear up in case you misread, I said java.time, not Joda-Time. Joda-Time will already be a step forward, but is in maintenance mode. java.time is related and is the more modern one of the two. – Ole V.V. Oct 16 '18 at 05:06
  • @OleV.V. sorry I misread Thanks for your suggestions :) – Aakriti Gupta Oct 16 '18 at 05:08

2 Answers2

1

tl;dr

LocalDate
.of( 2018 , Month.MAY , 23 )
.atStartOfDay(
    ZoneId.of( "America/New_York" )
)

Details

Never use the terrible old legacy date-time classes such as Date.

Use modern java.time classes.

ZoneId z = ZoneId.of( "America/New_York" ) ;
LocalDate ld = LocalDate.of( 2018 , Month.MAY , 23 ) ;
ZonedDateTime zdtStart = ld.atStartOfDay( z ) ;

When tracking an entire day, do not try to nail down the last moment. We commonly define a span of time using the Half-Open approach where the beginning is inclusive while the ending is exclusive. So a day starts with the first moment (typically at 00:00, but not always), and runs up to, but does not include, the first moment of the next day.

LocalDate dayAfter = ld.plusDays( 1 ) ;
ZonedDateTime zdtStop = dayAfter.atStartOfDay( z ) ;

Tip: Add the ThreeTen-Extra library to your project to access the Interval class.

org.threeten.extra.Interval interval = 
    Interval.of( 
        zdtStart.toInstant() ,
        zdtStop.toInstant()
    )
;

The class carries handy comparison methods such as abuts, contains, encloses, intersection, and more.

boolean containsMoment = interval.contains( Instant.now() ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

If you want IST timings, just change from EST to IST your line 2:

estFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Kishita Variya
  • 810
  • 9
  • 19