Last active
December 16, 2015 10:19
-
-
Save YukiYoshikawa/5419307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package trial.yy.java8.client.time; | |
import java.time.DateTimeException; | |
import java.time.Duration; | |
import java.time.LocalDateTime; | |
import java.time.Month; | |
import java.time.Period; | |
import java.time.format.DateTimeFormatter; | |
/** | |
* java8 java.time.LocalDateTimeを試すためのサンプル | |
* User: yy | |
* Date: 13/04/19 | |
* Time: 22:30 | |
*/ | |
public class Java8LocalDateTimeClient { | |
public static void main(String[] args) { | |
// 現在日時 | |
LocalDateTime date1 = LocalDateTime.now(); | |
System.out.println("date1 : " + date1); | |
// 年・月・日・時・分を指定(2013/12/31 15:30) | |
LocalDateTime date2 = LocalDateTime.of(2013, 12, 31, 15, 30); | |
System.out.println("date2 : " + date2); | |
// 年・月・日・時・分・秒を指定(2013/6/1 09:05:10) | |
LocalDateTime date3 = LocalDateTime.of(2013, Month.JUNE, 1, 9, 5, 10); | |
System.out.println("date3 : " + date3); | |
// 生成した日時の年・月・日・曜日を個別に取り出す | |
LocalDateTime date4 = LocalDateTime.of(2013, 1, 31, 23, 59, 1); | |
System.out.println("date4 : "); | |
System.out.println(" 年:" + date4.getYear()); | |
System.out.println(" 月:" + date4.getMonth()); | |
System.out.println(" 日:" + date4.getDayOfMonth()); | |
System.out.println(" 曜日:" + date4.getDayOfWeek()); | |
System.out.println(" 時:" + date4.getHour()); | |
System.out.println(" 分:" + date4.getMinute()); | |
System.out.println(" 秒:" + date4.getSecond()); | |
// 加減算してみる | |
LocalDateTime date5 = LocalDateTime.of(2013, 1, 31, 23, 59, 1); | |
System.out.println(date5 + "の1年後: " + date5.plusYears(1)); | |
System.out.println(date5 + "の2ヶ月後: " + date5.plusMonths(2)); | |
System.out.println(date5 + "の3日後: " + date5.plusDays(3)); | |
System.out.println(date5 + "の4時間後: " + date5.plusHours(4)); | |
System.out.println(date5 + "の5分後: " + date5.plusMinutes(5)); | |
System.out.println(date5 + "の6秒後: " + date5.plusSeconds(6)); | |
System.out.println(date5 + "の1年前: " + date5.minusYears(1)); | |
System.out.println(date5 + "の2ヶ月前: " + date5.minusMonths(2)); | |
System.out.println(date5 + "の3日前: " + date5.minusDays(3)); | |
System.out.println(date5 + "の4時間前: " + date5.minusHours(4)); | |
System.out.println(date5 + "の5分前: " + date5.minusMinutes(5)); | |
System.out.println(date5 + "の6秒前: " + date5.minusSeconds(6)); | |
// 期間を加減算してみる | |
LocalDateTime date6 = LocalDateTime.of(2013, 1, 1, 23, 59, 1); | |
// 3日間を加算・減算してみる | |
System.out.println(date6 + "の3日後: " + date6.plus(Period.ofDays(3))); | |
System.out.println(date6 + "の3日前: " + date6.minus(Period.ofDays(3))); | |
// 3時間を加算・減算してみる | |
System.out.println(date6 + "の3時間後: " + date6.plus(Duration.ofHours(3))); | |
System.out.println(date6 + "の3時間前: " + date6.minus(Duration.ofHours(3))); | |
// 日付文字列からparseしてみる | |
LocalDateTime date7 = | |
LocalDateTime.parse("2013/07/01 12:34:55", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")); | |
System.out.println("date7 : " + date7); | |
// LocalDateTimeからLocalDate、LocalTimeを取得 | |
LocalDateTime date8 = LocalDateTime.of(2013, 1, 3, 10, 12, 20); | |
System.out.println("date8 : " + date8); | |
System.out.println("date8から取得したLocalDate : " + date8.toLocalDate()); | |
System.out.println("date8から取得したLocalTime : " + date8.toLocalTime()); | |
// LocalDateTimeの各日時の要素を置き換えて新しいLocalTimeDateを取得する | |
LocalDateTime date9 = LocalDateTime.of(2013, 1, 3, 10, 12, 20); | |
System.out.println("date9 : " + date9); | |
System.out.println(date9 + " の年を2012に置き換え : " + date9.withYear(2012)); | |
System.out.println(date9 + " の日を31に置き換え : " + date9.withDayOfMonth(31)); | |
System.out.println(date9 + " の時を23に置き換え : " + date9.withHour(23)); | |
System.out.println(date9 + " の分を0に置き換え : " + date9.withMinute(0)); | |
System.out.println("###"); | |
// 存在しない2013年2月30日12時31分50秒とか指定するとDateTimeExceptionがスローされる | |
// (DateTimeExceptionは実行時例外なのも今っぽい気がします) | |
try { | |
LocalDateTime.of(2013, Month.FEBRUARY, 30, 12, 31, 50); | |
} catch (DateTimeException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment