java - How does Calendar class calculate months? -
i got annoying problem calendar class. have 2 jtextfields enter period of date (txtstart & txtend). if start date begins @ first day of month (01.), set end date "last day of month".
now user can change change period clicking plus or minus button, want increase or decrease month of start & end date.
calendar tempstart = calendar.getinstance(); calendar tempend = calendar.getinstance(); if (txtstart.gettext().trim().startswith("01.")) { system.out.println("get dates typed user, , set \"last day of month\" txtend"); tempstart = convstringtodate(txtstart.gettext().trim(), false); system.out.println(tempstart.gettime() + " #+#+###++ "); tempend = getlastdayofmonth(txtstart.gettext().trim()); system.out.println(tempend.gettime() + " #+#+###++ "); system.out.println(" "); system.out.println("multi either +1 or -1, increasing or decreasing month !"); tempstart.set(calendar.month, tempstart.get(calendar.month) + multi); system.out.println(tempstart.gettime() + " #+#+###++ "); tempend.set(calendar.month, tempend.get(calendar.month) + multi); system.out.println(tempend.gettime() + " #+#+###++ "); system.out.println(" "); }
my methods working correctly. got bewildering output.
if enter 01.11.2015 @ txtstart (dd.mm.yyy) got following output:
get dates typed user, , set "last day of month" txtend sun nov 01 00:00:01 gmt 2015 #+#+###++ mon nov 30 23:59:59 gmt 2015 #+#+###++ multi either +1 or -1, increasing or decreasing month ! tue dec 01 00:00:01 gmt 2015 #+#+###++ wed dec 30 23:59:59 gmt 2015 #+#+###++
looks pretty nice , everthing seems work correctly, if enter 01.10.2015 @ txtstart (dd.mm.yyy) got following output:
get dates typed user, , set "last day of month" txtend thu oct 01 00:00:01 gmt 2015 #+#+###++ sat oct 31 23:59:59 gmt 2015 #+#+###++ multi either +1 or -1, increasing or decreasing month ! sun nov 01 00:00:01 gmt 2015 #+#+###++ tue dec 01 23:59:59 gmt 2015 #+#+###++
may have idea why end date wrong @ output 2.
edit: multi = +1 or -1 (see in output1 or output2 comment)
private calendar getlastdayofmonth(string sdate) { calendar cal = convstringtodate(sdate, true); // passing month-1 because 0-->jan, 1-->feb... 11-->dec // calendar.set(year, month - 1, 1); cal.set(calendar.date, cal.getactualmaximum(calendar.date)); cal.set(calendar.hour_of_day, max_zeit[0]); // 23 cal.set(calendar.minute, max_zeit[1]); // 59 cal.set(calendar.second, max_zeit[2]); // 59 cal.set(calendar.millisecond, max_zeit[3]); // 0 // time: 23:59:59:0 return cal; }
############## solution: ####################. if (txtstart.gettext().trim().startswith("01.")) { tempstart = convstringtodate(txtstart.gettext().trim(), false); tempend = (calendar) tempstart.clone(); // set date somewhere @ same month ( e.g. @ start date ) tempstart.set(calendar.month, tempstart.get(calendar.month) + multi); // inc- or decrease month first tempend.set(calendar.month, tempend.get(calendar.month) + multi); // inc- or decrease month first ( there no overflow due 30th or 31th day ) tempend = getlastdayofmonth(df2.format(tempend.gettime())); // setting "last day of month" }
the solution first of increase or decrease month, after can set last day of month without getting overflow problems.
output:
get dates typed user, , set "last day of month" txtend thu oct 01 00:00:01 gmt 2015 #+#+###++ thu oct 01 00:00:01 gmt 2015 #+#+###++ multi either +1 or -1, increasing or decreasing month ! sun nov 01 00:00:01 gmt 2015 #+#+###++ sun nov 01 00:00:01 gmt 2015 #+#+###++ sun nov 01 00:00:01 gmt 2015 #+#+###++ mon nov 30 23:59:59 gmt 2015 #+#+###++
i thank !!!
the end date incorrect in first example, well. shows 30/12 whereas last day of december 31st. when add +1 month don't check whether following month has same number of days.
november has 30 days. therefore, incrementing october 31st gives november "31st" december 1st.
lots of programmers need arithmetic on dates. that's why java.util.calendar class has method add() can use encapsulates calculations need. check javadocs: http://docs.oracle.com/javase/7/docs/api/java/util/calendar.html#add(int,%20int)
Comments
Post a Comment