Generate Day start and Day end time between two dates in Java. Day light saving issue -


i have generate days between 2 two dates. ex:

1420090495000 -> jan 1, 2015

1430458495000 -> may 1, 2015

i have generate timestamps days like

jan 1, 2015 00:00:00 - jan 1, 2015 23:59:59

jan 2, 2015 00:00:00 - jan 2, 2015 23:59:59

jan 3, 2015 00:00:00 - jan 3, 2015 23:59:59

so on

i able that. getting problem day light saving issue. on march somewhere generating this

mar 8, 2015 00:00:00 - mar 9, 2015 00:01:00 wrong , should mar 8, 2015 00:00:00 - mar 8, 2015 23:59:59

i found because of day light saving issue. how solve issue ?

my code is:

public static list<string> getdatesrange(long start, long end, string tzoffset) { //tzoffset 420. usa          timezone tz = timezone.gettimezone(tzoffset);          list<string> dates=new arraylist();         calendar calendar = calendar.getinstance(tz);          calendar.set(calendar.day_of_week, 1);         calendar.set(calendar.hour, 0);         calendar.set(calendar.minute, 0);         calendar.set(calendar.second, 0);         calendar.set(calendar.millisecond, 0);           while (start<end) {             calendar.settimeinmillis(start);             long starttime = calendar.gettimeinmillis();             int year= calendar.getweekyear();               long endtime = start + (1 * 24 * 3600 * 1000l);              if(endtime<end) {                 endtime-=1000;                 system.out.println("start date= " + new date(new timestamp(start).gettime())+" ("+starttime+"), end date= "+new date(new timestamp(endtime).gettime())+"("+endtime+")");  dates.add(starttime+"-"+endtime);                 start= endtime+1000;             }             else{                 system.out.println("start date= " + new date(new timestamp(start).gettime()) + " (" + starttime + "), end date= " + new date(new timestamp(end).gettime()) + "(" + end + ")");                 start=end;                 dates.add(starttime+"-"+end);             }         }         return dates;     } 

there couple things wonder this:

  • why use longs define dates?
  • why return list of strings instead of list of dates?
  • why take timezone consideration @ in case this?

nevertheless, can achieve using calendar , simpledateformat, this:

public static calendar getdaystart(final long timeinmillis) {     final calendar cal = calendar.getinstance();     // end time date     cal.settimeinmillis(timeinmillis);      cal.set(calendar.hour, 0);     cal.set(calendar.minute, 0);     cal.set(calendar.second, 0);     cal.set(calendar.millisecond, 0);      return cal; }  public static list<string> getdatesrange(final long start, final long end) {      final calendar cal = getdaystart(start);     final date startdate = cal.gettime();      final calendar calend = getdaystart(end);     //adding 1 day because of strict comparison in while below     calend.add(calendar.day_of_year, 1);     final date enddate = calend.gettime();      final simpledateformat formatter = new simpledateformat("mmm d, yyyy hh:mm:ss");      final list<string> dates = new arraylist<string>();     final date dayend;     string currentday = "";      while(cal.gettime().before(enddate)) {         currentday = formatter.format(cal.gettime());         currentday += " - ";         //going end of day         cal.add(calendar.day_of_year, 1);         cal.add(calendar.second, -1);         currentday += formatter.format(cal.gettime());         //going next day again , continue loop         cal.add(calendar.second, 1);         //add computed list of days         dates.add(currentday);     }      return dates; } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -