java - Adding Days to Calendar -
i'm new site , have started learning java. i'm trying add couple days gregoriancalendar doesn't work. here... (ignore top chunk), adding dates @ bottom annoying.
/* * author:matt m * date:8.12.13 * discription: when user inputs deadline, , difficulity of project, * program gives date should start working on */ import java.util.*; public class deadlineplanner{ public static void main(string[] args) { //take information , restart questions if information wrong int month = 0, day = 0 ; { { system.out.println("input month please"); month = (new scanner(system.in).nextint() - 1); system.out.println("input day please"); day = (new scanner(system.in).nextint()); } while (!(month <= 12) || !(month >= 0)); } while (!(day <= 31) || !(month >= 0)); //make new calender , initialize gregoriancalendar setup = new gregoriancalendar(); setup.set(2013, month, day); system.out.println("the deadline "+ setup.gettime()); //switch statement give starting date system.out.println("is project hard or easy?"); scanner difficulity = new scanner(system.in); switch (difficulity.nextline()) { case "easy": setup.add(day, -1); system.out.print("the date should start workinng on "); system.out.println(setup.gettime()); break; case "hard": setup.add(day, -10); system.out.print("the date should start workinng on "); system.out.println(setup.gettime()); break; default: system.out.println("your answers questions incorrect"); break; } } }
thanks reading through this!i'm open feedback...
there's code here. user interaction.
start simple method 1 thing, work way out after right.
here's how might it:
public class dateutils { private dateutils() {} public static date adddays(date basedate, int daystoadd) { calendar calendar = calendar.getinstance(); calendar.settime(basedate); calendar.add(calendar.day_of_year, daystoadd); return calendar.gettime(); } }
once have method tested , proven can let rest of code call it.
update: it's 4 years later, , jdk 8 has given new joda-based time package. should using classes, not jdk 1.0 calendar
.
Comments
Post a Comment