java - Regarding adding days to the current date -


i newbie world of java. learning , exploring java calendar class abstract class consisting of factory method instance (). trying increment or decrement date, e.g. adding 1 day current date tomorrow's date, subtracting 1 day yesterday's date, etc. since date in java maintained long millisecond value, programmers tend add 24 hours 1 day. wrong if day falls on daylight saving timezone, day either 23 or 25 hours long. when add or subtract days date, other components of date e.g. month , year must roll.

my query shown below. in class trying add , subtract days. please advise, correct approach or there other better approach advise.

    //using calendar increment , decrement days date in java      date today = new date();      system.out.println("today " + toddmmyy(today));      calendar cal = calendar.getinstance(); //adding 1 day current date cal.add(calendar.day_of_month, 1);      date tommrrow = cal.gettime();      system.out.println("tomorrow " + toddmmyy(tommrrow)); //substracting 2 day date in java     cal.add(calendar.day_of_month, -2);  date yesterday = cal.gettime(); system.out.println("yesterday " + mmyy(cal.gettime())); 

your correct. happens when use date milliseconds arithmetic

     simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm");      date d1 = df.parse("2013-03-30 12:00");      date d2 = new date(d1.gettime() + 24 * 3600 * 1000);  // +24 hrs      system.out.println(df.format(d2)); 

output

2013-03-31 13:00 

Comments

Popular posts from this blog

asp.net mvc 3 - Using mvc3, I need to add a username/password to the sql connection string at runtime -

kineticjs - draw multiple lines and delete individual line -

thumbnails - jQuery image rotate on hover -