Saturday, 15 January 2011

Java Date year calculation is off by year for two days -



Java Date year calculation is off by year for two days -

this caused y2k-style bug in software if can imagine. unusual thing off-by-one year calculation occurs 2 days in year, i'm less sure how troubleshoot.

the output:

03-jan-2013 02-jan-2013 01-jan-2013 31-dec-2013 ** unusual 30-dec-2013 ** unusual 29-dec-2012 28-dec-2012 27-dec-2012 26-dec-2012 25-dec-2012

i not sure part of java date utilities cause such error.

the code (since test little included finish working program):

import java.util.calendar; import java.util.date; import java.text.simpledateformat; public class datet { private static string getformattedbackscanstarttime(int days) { simpledateformat dateformat = new simpledateformat("dd-mmm-yyyy"); calendar workingdate = calendar.getinstance(); workingdate.add(calendar.date, -1 * days); string formattedstarttime = dateformat.format(workingdate.gettime()); homecoming formattedstarttime; } public static void main(string args[]) { for(int = 35; < 45; i++) { system.out.println(getformattedbackscanstarttime(i)); } } }

this problem:

"dd-mmm-yyyy"

yyyy week-year, not calendar year. want yyyy instead.

the lastly 2 days of calendar year 2012 in first week of week-year 2013. should utilize week year in conjunction "week of year" specifier (w).

java date calendar simpledateformat

No comments:

Post a Comment