difference between two dates in java in 'EST' time zone -
i have parse string date, let givendate
, est timezone. also, have take current date, let currentdate
in est timezone. current timezone ist (gmt+5:30).
if currentdate greater givendate need add day currentdate.
here solution have implemented.
simpledateformat df = new simpledateformat("mm/dd/yyyy"); calendar givendate = calendar.getinstance(timezone.gettimezone("est")); givendate .settime(df.parse("01/08/2016")); calendar currentdate = calendar.getinstance(timezone.gettimezone("est")); if(currentdate.get(calendar.year) > givendate .get(calendar.year) || currentdate.get(calendar.day_of_year) > givendate.get(calendar.day_of_year)){ currentdate.add(calendar.date, 1); } system.out.println("final date : "+df.format(currentdate.gettime()));
is there better solution this?
also, following has been tickling mind.
consider code snippet
simpledateformat df = new simpledateformat("mm/dd/yyyy"); calendar givendate = calendar.getinstance(timezone.gettimezone("est")); givendate .settime(df.parse("01/07/2016")); system.out.println(givendate .get(calendar.date)); //6 system.out.println(givendate .get(calendar.day_of_month)); //6 system.out.println(givendate .get(calendar.day_of_year)); //6
why getting 6 date, day_of_month, day_of_year? expect must return 7 given date 7 days starting of year 2016.
but if add following line after first line of above code, above mentioned fields returning 7. reason of this?
df.settimezone(timezone.gettimezone("est"));
.
you need set timezone simpledateformat class, , can compare parsed date current time:
simpledateformat df = new simpledateformat("mm/dd/yyyy"); df.settimezone(timezone.gettimezone("est")); date givendate = df.parse("01/08/2016"); calendar currentdate = calendar.getinstance(); if(currentdate.gettime().compareto(givendate) > 0) { currenttime.add(calendar.date, 1); }
keep in mind date object represents current time (number of seconds since epoch) in utc timezone.
Comments
Post a Comment