Javaã§ã¯ãããããã便å©ãªAPIã¯ã©ã¹ã使ããããã«ãªã£ã¦ãã¾ããåºæ¬çã«ã¯ãimport宣è¨ã§èªã¿è¾¼ãå½¢ã§APIã®å©ç¨ãã§ãã¾ãã
â© Â APIã¯ã©ã¹ã®æ å ±ã¯ä¸è¨ãµã¤ãã§èª¿ã¹ããã¾ãï¼ãã¼ã¸ã§ã³8ã®ä¾ï¼
Dateã¯ã©ã¹
ç¶æ¿é¢ä¿
æ¥ä»ã®è¨ç®ãªã©ãå¿ è¦ãªãå ´åã¯ãDateã¯ã©ã¹ã使ãã®ãè¯ãããã§ãã
ã®2種é¡ã®APIãããã¾ãããJava.util.Dateã使ã£ã¦ããã¾ãã
package chap14; import java.util.Date; public class DateSample { public static void main(String[] args) { // ç¾å¨æ¥æã®åå¾ Date now = new Date(); System.out.println(now); // longå¤ã¨ãã¦åå¾ System.out.println(now.getTime()); // longå¤ãæå®ãã¦æ¥æãç¹å® Date past = new Date(1499389966046L); System.out.println(past); } }
Â
Calendarã¯ã©ã¹
ç¶æ¿é¢ä¿
æ¥ä»ã®è¨ç®ãªã©ãè¡ãå ´åã«ã¯ãCalendarã¯ã©ã¹ãè¯ãããã§ãã
package chap14; import java.util.Calendar; import java.util.Date; public class CalendarSample { public static void main(String[] args) { String[] week = {"æ¥","æ","ç«","æ°´","æ¨","é","å"}; // ç¾å¨ã®å¹´ã表示ãã // Date now = new Date(); Calendar c = Calendar.getInstance(); // c.setTime(now); int y = c.get(Calendar.YEAR); // å¹´ã®åå¾ int m = c.get(Calendar.MONTH) + 1; // æã®åå¾ System.out.println("ç¾å¨ã¯" +y + "å¹´" + m + "æ"); // åå¾ãããæ¥ææ å ±ãintå¤ã¨ãã¦åå¾ãã System.out.println(c.get(Calendar.YEAR) + "å¹´" + (c.get(Calendar.MONTH) + 1) + "æ" + c.get(Calendar.DAY_OF_MONTH) + "æ¥" + c.get(Calendar.HOUR_OF_DAY) + "æ" + c.get(Calendar.MINUTE) + "å" + c.get(Calendar.SECOND) + "ç§ " + week[c.get(Calendar.DAY_OF_WEEK) - 1] + "ææ¥"); // æå®ããæ¥ã®Dateåã®å¤ãå¾ã c.set(2010, 8, 22, 1, 23, 45); // Calendarã«æ¥æãè¨å®ãã c.set(Calendar.YEAR, 2011); // å¹´ã ãã2011å¹´ã«å¤æ´ Date past = c.getTime(); // Dateåã«å¤æ System.out.println(past); } }
Â
SimpleDateFormatã¯ã©ã¹
ç¶æ¿é¢ä¿
Â
package chap14; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatSample { public static void main(String[] args) throws ParseException { // æ¬æ¥ã®æ¥æã表示ãã Date now = new Date(); SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // åºåãã©ã¼ãããã®æå® String s = f.format(now); System.out.println(s); SimpleDateFormat sdf = new SimpleDateFormat("yyyyå¹´MMæddæ¥ Eææ¥ HHæmmåssç§"); // åºåãã©ã¼ãããã®æå® String s1 = sdf.format(now); System.out.println(s1); // æå®æ¥æã®æååã解æãDateåã¨ãã¦å¾ã Date d = f.parse("2011/09/22 01:23:55"); System.out.println(d); System.out.println(f.format(d)); } }
æ¥ä»ã®åºåå½¢å¼ãå¤æ´ã§ãã¾ãã
Java 8ã§è¿½å ãããJava.timeããã±ã¼ã¸
ãããã§ã¯ãDate-Time API ã¨å¼ã°ããããã¦ãããã§ãã
Java.timeããã±ã¼ã¸ãå°å ¥ãããã¾ã§ã¯ãDate / Calendar / SimpleDateFormat ãªã©ã®ã¯ã©ã¹ã使ã£ã¦ããããã§ãããããããã¯ãJava.timeããã±ã¼ã¸ãå©ç¨ãã¦ããæµãã«ãªãããã§ãã
- æå·®ãæ±ããªãã®ãªãã°ãLocalã§å§ã¾ãã¯ã©ã¹ã使ã
- æå·®ãæ±ãã®ãªãã°ãOffsetã§å§ã¾ãã¯ã©ã¹ã使ã
- æå·®ã«å ãã¦ããã®å°åã®æ¥æãèæ ®ããã¦ããæ¹ãæã¾ããå ´åã¯ZoneDateTimeã¯ã©ã¹ã使ã
- Stringã¯ã©ã¹ã¨åæ§ãã¤ãã¥ã¼ã¿ãã«ã§ã¹ã¬ããã»ã¼ããªã¯ã©ã¹ã«ãªã£ã
- ããã¾ã§ããªç§ã¾ã§ããæ±ããªãã£ãã®ããããç§ã¾ã§æ±ããããã«ãªã£ã
Â
java.timeããã±ã¼ã¸ã®LocaleDateã¯ã©ã¹
LocaleDateã¯ã©ã¹
ç¶æ¿é¢ä¿
Â
package api; import java.time.LocalDate; public class LocalDateSample { public static void main(String[] args) { // æå·®ãæããªãæ¥æ LocalDate today = LocalDate.now(); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); LocalDate future_day = today.plusDays(5); LocalDate past_day = today.minusDays(5); if(today.isBefore(future_day)) { System.out.println("today < future_day"); } if(today.isAfter(past_day)) { System.out.println("past_day < today"); } if(today.isEqual(today)) { System.out.println("today = today"); } System.out.println(today); System.out.println(year + "å¹´" + month + "æ" + day + "æ¥"); System.out.println(future_day); System.out.println(past_day); } }
Â
java.timeããã±ã¼ã¸ã®åæåDayOfWeek
åæåDayOfWeek
ç¶æ¿é¢ä¿
LocaleDateã¯ã©ã¹ã®ã¡ã½ããã§ãææ¥ãåå¾ã§ããã®ã§ãããææ¥ç¨ã®å¤æ°ãç¨æãã¦ãããªãã¨ãããªãããã§ããããDayOfWeekã¯ã©ã¹ã§ãã
package api; import java.time.DayOfWeek; import java.time.LocalDate; public class DayOfWeekSample { public static void main(String[] args) { // æå·®ãæããªãæ¥æ LocalDate today = LocalDate.now(); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); // ææ¥ DayOfWeek dayWeek = today.getDayOfWeek(); System.out.println(today); System.out.println(year + "å¹´" + month + "æ" + day + "æ¥" + dayWeek); } }
ãã®ã¾ã¾ã ã¨ãææ¥ãè±èªã«ãªã£ã¦ãã¾ã£ã¦ããã®ã§æ¥æ¬èªã«å¤æãããã¨ããã§ãã
java.time.formatããã±ã¼ã¸ã®åæåTextStyleãJava.util.Locale
åæåTextStyle
- java.time.format
ç¶æ¿é¢ä¿
Localeã¯ã©ã¹
ç¶æ¿é¢ä¿
package api; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.TextStyle; import java.util.Locale; public class DayOfWeekSample { public static void main(String[] args) { // æå·®ãæããªãæ¥æ LocalDate today = LocalDate.now(); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); // ææ¥ DayOfWeek dayWeek = today.getDayOfWeek(); String str_dayWeek = dayWeek.getDisplayName(TextStyle.FULL, Locale.JAPAN); System.out.println(today); System.out.println(year + "å¹´" + month + "æ" + day + "æ¥" + str_dayWeek); } }
java.time.formatããã±ã¼ã¸ã®åæåTextStyleã¨Java.utilããã±ã¼ã¸ã®Localeã¯ã©ã¹ãå¿ è¦ã§ãã
LocalTimeã¯ã©ã¹
ç¶æ¿é¢ä¿
package api; import java.time.LocalTime; public class LocalTimeSample { public static void main(String[] args) { int hour = 7; int minutes = 15; int seconds = 30; // æå»ãä½æ LocalTime time = LocalTime.of(hour, minutes, seconds); System.out.println(time); } }
ã¡ã½ãããªã©ã¯LocalDateã¨ã»ã¨ãã©åãæãã§ãããã
LocalDateTimeã¯ã©ã¹
LocalDateTimeã¯ã©ã¹
ç¶æ¿é¢ä¿
package api; import java.time.LocalDateTime; public class LocalDateTimeSample { public static void main(String[] args) { int year = 2017; int month = 07; int dayOfMonth = 9; int hour = 7; int minute = 15; int second = 30; int nanoOfSecond = 000000001; // æå»ãä½æ LocalDateTime dateTime = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); System.out.println(dateTime); } }
ã¡ã½ãããªã©ã¯LocalDateã¨ã»ã¨ãã©åãæãã§ãããã
Durationã¯ã©ã¹
LocalDateTimeã¯ã©ã¹
ç¶æ¿é¢ä¿
package api; import java.time.Duration; import java.time.LocalDateTime; public class DurationSample { public static void main(String[] args) { int year = 2017; int month = 07; int dayOfMonth = 9; int hour = 7; int minute = 15; int second = 30; int nanoOfSecond = 000000001; // æå»ãä½æ LocalDateTime startInclusive = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); LocalDateTime endExclusive = LocalDateTime.of(year + 1, month + 1, dayOfMonth + 1, hour + 1, minute, second + 1, nanoOfSecond + 1); Duration duration = Duration.between(startInclusive, endExclusive); System.out.println("ããªç§ããããç§ã®å·®ï¼" + duration.getNano()); System.out.println("ç©ç®æ¥æ°ã®å·®ï¼" + duration.toDays()); System.out.println("ç©ç®æéã®å·®ï¼" + duration.toHours()); System.out.println("ç©ç®åæ°ã®å·®ï¼" + duration.toMinutes()); System.out.println("ç©ç®ç§æ°ã®å·®ï¼" + duration.getSeconds()); System.out.println("ç©ç®ããªç§ã®å·®ï¼" + duration.toMillis()); System.out.println("ç©ç®ããç§ã®å·®ï¼" + duration.toNanos()); } }
æå»ã®ééãæ±ãããã§ãã
Periodã¯ã©ã¹
ç¶æ¿é¢ä¿
package api; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Period; public class PeriodSample { public static void main(String[] args) { int year = 2017; int month = 07; int dayOfMonth = 9; int hour = 7; int minute = 15; int second = 30; int nanoOfSecond = 000000001; // æå»ãä½æ LocalDateTime localDateStart = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); LocalDateTime localDateEnd = LocalDateTime.of(year + 1, month + 1, dayOfMonth + 1, hour + 1, minute, second + 1, nanoOfSecond + 1); LocalDate startDateInclusive = localDateStart.toLocalDate(); LocalDate endDateExclusive = localDateEnd.toLocalDate(); Period period = Period.between(startDateInclusive, endDateExclusive); System.out.println("å¹´æ°ã®å·®ï¼" + period.getYears()); System.out.println("ææ°ã®å·®ï¼" + period.getMonths()); System.out.println("æ¥æ°ã®å·®ï¼" + period.getDays()); System.out.println("ç©ç®ææ°ã®å·®ï¼" + period.toTotalMonths()); } }
æ¥ä»ã®ééãæ±ãããã§ãã
Date Time APIã¨Date / Calendar ã®ç¸äºå¤æ
java.time.instantã¯ã©ã¹ã¨ãããã®ã使ããã¨ã§ãDateã¯ã©ã¹ãCalendarã¯ã©ã¹ã§è¨è¿°ããã¦ãããã®ãæ¸ãæãããã¨ãã§ããã¿ããã§ãã
package api; import java.time.Instant; import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZoneOffset; import java.util.Calendar; import java.util.Date; public class TimeInstaceSample { public static void main(String[] args) { // Date date = new Date(); Calendar cl = Calendar.getInstance(); int year = 2017; int month = Month.JULY.getValue(); int day = Calendar.DAY_OF_MONTH; int hour = Calendar.HOUR; int minute = Calendar.MINUTE; int second = Calendar.SECOND; int mills = Calendar.MILLISECOND; int nano = 567890123; LocalDateTime localDateTime = LocalDateTime.of(year, month, day, hour, minute, second, nano); Instant instant = localDateTime.toInstant(ZoneOffset.of("+09:00")); System.out.println(localDateTime); // Instant â Date å¤æ Date date2 = Date.from(instant); System.out.println(date2); // Date â Instant å¤æ Instant instant2 = date.toInstant(); LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant2, ZoneId.of("Asia/Tokyo")); System.out.println(instant2); System.out.println(localDateTime2); // Calendar â Instant Instant instant3 = cl.toInstant(); LocalDateTime localDateTime3 = LocalDateTime.ofInstant(instant3, ZoneId.of("Asia/Tokyo")); System.out.println(instant3); System.out.println(localDateTime3); } }
ãã ããDateãCalendarã¯ã©ã¹ã§ã¯ããªç§ã¾ã§ããæ±ããªãã®ã§ãDate-Time APIã®ã¯ã©ã¹ãããDate / Calendar ã¯ã©ã¹ã«å¤æããã¨ãã¼ã¿ã®åãæ¨ã¦ãçºçããããã§ãã
ä»åã¯ãã®ã¸ãã§ãÂ
Â
Â