java 8 time包与joda 对比

    xiaoxiao2021-11-30  18

    joda 适用于 java 5,java 6 ,Java 7

    import org.joda.time.LocalDate; import org.joda.time.DateTimeZone; import org.joda.time.LocalDate; /** * joda * * @author ibm * */ public class App2 { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println("Today's Local date : " + today); int year = today.getYear(); int month = today.getMonthOfYear(); int day = today.getDayOfMonth(); System.out.printf("Year : %d Month : %d day : %d \t %n", year, month, day); /** * 创建本地日期 */ LocalDate dateOfBirth = new LocalDate(2010, 1, 12); System.out.println("Your Date of birth is : " + dateOfBirth); /** * 获取一周后的日期 */ LocalDate nextWeek = today.plusWeeks(1); System.out.println("Today is : " + today); System.out.println("Date after 1 week : " + nextWeek); /** * 日期前后判断 */ LocalDate tomorrow = new LocalDate(2016, 11, 19); if(tomorrow.isAfter(today)){ System.out.println("Tomorrow comes after today"); } LocalDate yesterday = today.minusDays(1); if(yesterday.isBefore(today)){ System.out.println("Yesterday is day before today"); } /** * 创建包含时区的日期时间 */ DateTimeZone zone = DateTimeZone.forID("America/New_York"); DateTime dateAndTimeInNewYork = new DateTime(null, zone); System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork); } }
    import java.time.LocalDate; import java.time.temporal.ChronoUnit; /** * * @author ibm * */ public class Test { public static void main(String[] args) throws InterruptedException { LocalDate today = LocalDate.now(); System.out.println("Today's Local date : " + today); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); System.out.printf("Year : %d Month : %d day : %d \t %n", year, month, day); /** * 创建本地日期 */ LocalDate dateOfBirth = LocalDate.of(2010, 1, 14); System.out.println("Your Date of birth is : " + dateOfBirth); /** * 获取一周后的日期 */ LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS); System.out.println("Today is : " + today); System.out.println("Date after 1 week : " + nextWeek); /** * 日期前后判断 */ LocalDate tomorrow = LocalDate.of(2016, 11, 19); if(tomorrow.isAfter(today)){ System.out.println("Tomorrow comes after today"); } LocalDate yesterday = today.minus(1, ChronoUnit.DAYS); if(yesterday.isBefore(today)){ System.out.println("Yesterday is day before today");   Instant instant = Instant.now(); ZonedDateTime zonew = ZonedDateTime.ofInstant(instant, ZoneId.of("America/New_York")); System.out.println(zonew); } } }
    转载请注明原文地址: https://ju.6miu.com/read-679108.html

    最新回复(0)