アットウィキロゴ

KS > practice > date

  1. package part1.greet;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.GregorianCalendar;
  8.  
  9. public class date {
  10.  
  11. public static void main(String[] args) throws IsSleepingException {
  12. Date now = new Date();
  13. DateFormat year = new SimpleDateFormat("yyyy");
  14. DateFormat month = new SimpleDateFormat("MM");
  15. DateFormat day = new SimpleDateFormat("dd");
  16. int yy = Integer.parseInt(year.format(now));
  17. int mm = Integer.parseInt(month.format(now)) - 1;
  18. int dd = Integer.parseInt(day.format(now));
  19.  
  20. //おはようございます
  21. Calendar c1 = new GregorianCalendar(yy,mm,dd,05,29,59);
  22. Calendar c2 = new GregorianCalendar(yy,mm,dd,11,00,00);
  23. //こんにちは
  24. Calendar c3 = new GregorianCalendar(yy,mm,dd,10,59,59);
  25. Calendar c4 = new GregorianCalendar(yy,mm,dd,17,00,00);
  26. //こんばんは
  27. Calendar c5 = new GregorianCalendar(yy,mm,dd,16,59,59);
  28. Calendar c6 = new GregorianCalendar(yy,mm,dd,21,30,00);
  29. //おやすみなさい
  30. Calendar c7 = new GregorianCalendar(yy,mm,dd,21,29,59);
  31. Calendar c8 = new GregorianCalendar(yy,mm,dd+1,00,00,00);
  32. Calendar c9 = new GregorianCalendar(yy,mm,dd-1,23,59,59);
  33. Calendar c10 = new GregorianCalendar(yy,mm,dd,01,30,00);
  34. //就寝中です!
  35. // Calendar c11 = new GregorianCalendar(yy,mm,dd,01,29,59);
  36. // Calendar c12 = new GregorianCalendar(yy,mm,dd,05,30,00);
  37.  
  38. Calendar c = new GregorianCalendar(yy,mm,dd,01,30,00);
  39. Date now1 = c.getTime();
  40.  
  41. if(now1.after(c1.getTime()) && now1.before(c2.getTime())) {
  42. System.out.println("おはようございます");
  43. } else if(now1.after(c3.getTime()) && now1.before(c4.getTime())) {
  44. System.out.println("こんにちは");
  45. } else if(now1.after(c5.getTime()) && now1.before(c6.getTime())) {
  46. System.out.println("こんばんは");
  47. } else if(now1.after(c7.getTime()) && now1.before(c8.getTime()) || now1.after(c9.getTime()) && now1.before(c10.getTime()) ) {
  48. System.out.println("おやすみなさい");
  49. } else {
  50. System.out.println("就寝中です!");
  51. throw new IsSleepingException();
  52. }
  53.  
  54. }
  55. }
  56.  
最終更新:2013年05月26日 16:30