Advertisement
cd62131

ParseDate

Feb 23rd, 2014
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.40 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.Locale;
  6. import java.util.Scanner;
  7.  
  8. public class ParseDate {
  9.   private Calendar calendar;
  10.   private DateFormat df;
  11.  
  12.   public ParseDate(String date) {
  13.     Date sd = null;
  14.     try {
  15.       sd = new SimpleDateFormat("yyyy-MM-dd").parse(date);
  16.     }
  17.     catch (Exception e) {
  18.     }
  19.     calendar = Calendar.getInstance(Locale.JAPAN);
  20.     calendar.setTime(sd);
  21.     df = DateFormat.getDateInstance(DateFormat.FULL, Locale.JAPAN);
  22.   }
  23.  
  24.   public Calendar getCalendar() {
  25.     return calendar;
  26.   }
  27.  
  28.   public String getWeek() {
  29.     int week = calendar.get(Calendar.DAY_OF_WEEK);
  30.     if (week == Calendar.MONDAY) return "Monday";
  31.     if (week == Calendar.TUESDAY) return "Tuesday";
  32.     if (week == Calendar.WEDNESDAY) return "Wednesday";
  33.     if (week == Calendar.THURSDAY) return "Thursday";
  34.     if (week == Calendar.FRIDAY) return "Friday";
  35.     if (week == Calendar.SATURDAY) return "Saturday";
  36.     return "Sunday";
  37.   }
  38.  
  39.   public void add(int days) {
  40.     calendar.add(Calendar.DATE, days);
  41.   }
  42.  
  43.   @Override
  44.   public String toString() {
  45.     return df.format(calendar.getTime());
  46.   }
  47.  
  48.   public static void main(String[] args) {
  49.     Scanner in = new Scanner(System.in);
  50.     ParseDate p = new ParseDate(in.next());
  51.     System.out.println(p);
  52.     in.close();
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement