public class LocalDateTest {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println(date);
// 101th day of 2014
LocalDate date1 = LocalDate.ofYearDay(2014, 101);
System.out.println(date1);
LocalDate date2 = LocalDate.ofEpochDay(1);
System.out.println(date2);
String start = "10-12-2018 21:04:05";
String end = "10-12-2018 21:06:51";
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime st = LocalDateTime.parse(start, format);
LocalDateTime et = LocalDateTime.parse(end, format);
Duration duration = Duration.between(st, et);
LocalTime time = LocalTime.ofSecondOfDay(duration.getSeconds());
System.out.println(time);
}
}
=====================
public class OldDateTime {
public static void main(String[] args) throws ParseException {
SimpleDateFormat formate = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date1 = new Date();
System.out.println(date1); // Sat Oct 13 15:54:14 IST 2018
System.out.println(date1.getTime()); // 1539426431470
Set<String> cal = Calendar.getAvailableCalendarTypes();
System.out.println(cal); // [gregory, buddhist, japanese]
System.out.println(TimeZone.getDefault());
// id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null
System.out.println(TimeZone.getTimeZone("Asia/Tokyo"));
// id="Asia/Tokyo",offset=32400000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null
System.out.println(formate.format(date1));
System.out.println(date1.getMonth());
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime()); // Sun Oct 14 12:11:24 IST 2018
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 287
}
}
========================
public class NewDateTime {
public static void main(String[] args) {
// we can get minus and plus hours, minutes, etc
LocalTime localTime = LocalTime.now();
System.out.println("Local Time : "+localTime); // 17:08:55.086
LocalDate localDate = LocalDate.now();
System.out.println("Local Date : "+localDate); // 2018-10-13
//We can get minus and plus Days, Weeks, Years, Months
LocalDate yesterdayDate = localDate.minusDays(1);
System.out.println(yesterdayDate); // 2018-10-12
LocalDate tomorrowDate = localDate.plusDays(1);
System.out.println(tomorrowDate); // 2018-10-14
// print another zoned time
ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
LocalTime tokyo = LocalTime.now(tokyoZone);
System.out.println("Tokyo Time "+tokyo); // Tokyo Time 22:14:52.155
// We can get time and date differences
long diff = ChronoUnit.DAYS.between(yesterdayDate, tomorrowDate);
System.out.println(diff);
// We can get Combination of Date and Time with different formats, using this we can get another zoned time and differences, plus, minus etc same as LocalDate and LocalTime classes
LocalDateTime datetime = LocalDateTime.now();
System.out.println(datetime); // 2018-10-13T19:14:12.639
LocalDateTime zoneDatetime = LocalDateTime.now(tokyoZone);
System.out.println(zoneDatetime); // 2018-10-13T22:48:18.751
long dateTimeDiff = ChronoUnit.HOURS.between(datetime, zoneDatetime);
System.out.println(dateTimeDiff);
MonthDay monthDay = MonthDay.now();
LocalDate date = monthDay.atYear(2017);
System.out.println("Month Day : "+date);
// we get hour, year etc everything from get methods and using fields
System.out.println("Get Hour : "+ChronoField.HOUR_OF_DAY.getFrom(datetime));
// change date format, we have different date formats
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(datetime.format(format)); // 13-10-2018 21:50:46
OffsetDateTime offsetDate = OffsetDateTime.now();
System.out.println(offsetDate);
System.out.println("Get Year : "+offsetDate.getYear());
System.out.println(offsetDate.toLocalDate());
// we can get year, month, date from specific date as like below
LocalDate getY = LocalDate.parse("2017-03-12");
System.out.println(getY.getYear());
Clock clock = Clock.systemDefaultZone();
System.out.println("Clock : "+clock);
Clock clocktime = Clock.systemUTC();
System.out.println("Clock Time : "+clocktime.instant());
Year year = Year.now();
System.out.println("Year : "+year);
YearMonth ym = YearMonth.now();
System.out.println("Year Month : "+ym);
MonthDay md = MonthDay.now();
System.out.println("Month Day : "+md);
ZonedDateTime zone = ZonedDateTime.now();
System.out.println(zone);
LocalTime localTime2 = LocalTime.parse("14:26:02.363");
System.out.println(localTime2); // 17:08:55.086
Duration dur = Duration.between(localTime, localTime2);
System.out.println("Duration : "+dur.toMinutes());
Duration duration = Duration.ofMinutes(2);
System.out.println(duration.getSeconds());
Period period = Period.between(getY, localDate);
System.out.println("Period : "+period.getMonths());
LocalDate p1 = LocalDate.parse("2017-03-12");
LocalDate p2 = LocalDate.now();
Period pdiff = Period.between(p1, p2);
System.out.println("Period : "+pdiff);
OffsetDateTime off_dt = OffsetDateTime.now();
System.out.println("OffsetDateTime : "+off_dt);
OffsetTime offtime = OffsetTime.now(clocktime);
System.out.println("OffsetTime : "+offtime);
long iso = IsoFields.WEEK_BASED_YEAR.getFrom(localDate);
System.out.println(iso);
}
}
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println(date);
// 101th day of 2014
LocalDate date1 = LocalDate.ofYearDay(2014, 101);
System.out.println(date1);
LocalDate date2 = LocalDate.ofEpochDay(1);
System.out.println(date2);
String start = "10-12-2018 21:04:05";
String end = "10-12-2018 21:06:51";
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime st = LocalDateTime.parse(start, format);
LocalDateTime et = LocalDateTime.parse(end, format);
Duration duration = Duration.between(st, et);
LocalTime time = LocalTime.ofSecondOfDay(duration.getSeconds());
System.out.println(time);
}
}
=====================
public class OldDateTime {
public static void main(String[] args) throws ParseException {
SimpleDateFormat formate = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date1 = new Date();
System.out.println(date1); // Sat Oct 13 15:54:14 IST 2018
System.out.println(date1.getTime()); // 1539426431470
Set<String> cal = Calendar.getAvailableCalendarTypes();
System.out.println(cal); // [gregory, buddhist, japanese]
System.out.println(TimeZone.getDefault());
// id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null
System.out.println(TimeZone.getTimeZone("Asia/Tokyo"));
// id="Asia/Tokyo",offset=32400000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null
System.out.println(formate.format(date1));
System.out.println(date1.getMonth());
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime()); // Sun Oct 14 12:11:24 IST 2018
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 287
}
}
========================
public class NewDateTime {
public static void main(String[] args) {
// we can get minus and plus hours, minutes, etc
LocalTime localTime = LocalTime.now();
System.out.println("Local Time : "+localTime); // 17:08:55.086
LocalDate localDate = LocalDate.now();
System.out.println("Local Date : "+localDate); // 2018-10-13
//We can get minus and plus Days, Weeks, Years, Months
LocalDate yesterdayDate = localDate.minusDays(1);
System.out.println(yesterdayDate); // 2018-10-12
LocalDate tomorrowDate = localDate.plusDays(1);
System.out.println(tomorrowDate); // 2018-10-14
// print another zoned time
ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
LocalTime tokyo = LocalTime.now(tokyoZone);
System.out.println("Tokyo Time "+tokyo); // Tokyo Time 22:14:52.155
// We can get time and date differences
long diff = ChronoUnit.DAYS.between(yesterdayDate, tomorrowDate);
System.out.println(diff);
// We can get Combination of Date and Time with different formats, using this we can get another zoned time and differences, plus, minus etc same as LocalDate and LocalTime classes
LocalDateTime datetime = LocalDateTime.now();
System.out.println(datetime); // 2018-10-13T19:14:12.639
LocalDateTime zoneDatetime = LocalDateTime.now(tokyoZone);
System.out.println(zoneDatetime); // 2018-10-13T22:48:18.751
long dateTimeDiff = ChronoUnit.HOURS.between(datetime, zoneDatetime);
System.out.println(dateTimeDiff);
MonthDay monthDay = MonthDay.now();
LocalDate date = monthDay.atYear(2017);
System.out.println("Month Day : "+date);
// we get hour, year etc everything from get methods and using fields
System.out.println("Get Hour : "+ChronoField.HOUR_OF_DAY.getFrom(datetime));
// change date format, we have different date formats
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(datetime.format(format)); // 13-10-2018 21:50:46
OffsetDateTime offsetDate = OffsetDateTime.now();
System.out.println(offsetDate);
System.out.println("Get Year : "+offsetDate.getYear());
System.out.println(offsetDate.toLocalDate());
// we can get year, month, date from specific date as like below
LocalDate getY = LocalDate.parse("2017-03-12");
System.out.println(getY.getYear());
Clock clock = Clock.systemDefaultZone();
System.out.println("Clock : "+clock);
Clock clocktime = Clock.systemUTC();
System.out.println("Clock Time : "+clocktime.instant());
Year year = Year.now();
System.out.println("Year : "+year);
YearMonth ym = YearMonth.now();
System.out.println("Year Month : "+ym);
MonthDay md = MonthDay.now();
System.out.println("Month Day : "+md);
ZonedDateTime zone = ZonedDateTime.now();
System.out.println(zone);
LocalTime localTime2 = LocalTime.parse("14:26:02.363");
System.out.println(localTime2); // 17:08:55.086
Duration dur = Duration.between(localTime, localTime2);
System.out.println("Duration : "+dur.toMinutes());
Duration duration = Duration.ofMinutes(2);
System.out.println(duration.getSeconds());
Period period = Period.between(getY, localDate);
System.out.println("Period : "+period.getMonths());
LocalDate p1 = LocalDate.parse("2017-03-12");
LocalDate p2 = LocalDate.now();
Period pdiff = Period.between(p1, p2);
System.out.println("Period : "+pdiff);
OffsetDateTime off_dt = OffsetDateTime.now();
System.out.println("OffsetDateTime : "+off_dt);
OffsetTime offtime = OffsetTime.now(clocktime);
System.out.println("OffsetTime : "+offtime);
long iso = IsoFields.WEEK_BASED_YEAR.getFrom(localDate);
System.out.println(iso);
}
}





