Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import java.util.*;
  2. import java.text.SimpleDateFormat;
  3.  
  4. public class Main {
  5. public static void main(String[] args) throws Exception {
  6.  
  7. // 『スラスラわかるJava』
  8. // 第5章-1 日付
  9.  
  10. Calendar calendar = Calendar.getInstance();
  11. Date today = calendar.getTime();
  12. System.out.println(today); //実行結果例 Sun Apr 14 05:36:48 UTC 2019
  13.  
  14. SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy/MM/dd HH;mm:ss");
  15. String formatDate1 = fmt1.format(calendar.getTime());
  16. System.out.println(formatDate1); //実行結果例 2019/04/14 05;36:48
  17.  
  18. SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy年MM月dd日 (E)", new Locale("en", "US"));
  19. String formatDate2 = fmt2.format(calendar.getTime());
  20. System.out.println(formatDate2); //実行結果例 2019年04月14日 (Sun)
  21.  
  22. SimpleDateFormat fmt3 = new SimpleDateFormat("yyyy年MM月dd日 (E)", new Locale("ja", "JP"));
  23. String formatDate3 = fmt3.format(calendar.getTime());
  24. System.out.println(formatDate3); //実行結果例 2019年04月14日 (日)
  25.  
  26. calendar.setTimeZone(TimeZone.getTimeZone("America/Los_angels"));
  27. System.out.println(calendar.getTime()); //実行結果例 Sun Apr 14 05:36:48 UTC 2019
  28.  
  29. calendar.clear();
  30. calendar.set(1234, 4, 6);
  31. System.out.println(calendar.getTime()); //実行結果例 Sat May 06 00:00:00 UTC 1234
  32.  
  33. calendar.set(Calendar.HOUR_OF_DAY, 12);
  34. System.out.println(calendar.getTime()); //実行結果例 Sat May 06 12:00:00 UTC 1234
  35.  
  36. calendar.add(Calendar.DAY_OF_MONTH, 3);
  37. System.out.println(calendar.getTime()); //実行結果例 Tue May 09 12:00:00 UTC 1234
  38.  
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement