• Date


    package test;
    
    import java.time.LocalDateTime;
    
    //取得当前的日期时间
    public class GetDatetime {
        public static void main(String[] args) {
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);  //2018-07-27T15:14:09.016
        }
    }
    package test;
    import java.time.LocalDate;
    import java.time.MonthDay;
    //判断是否闰年
    import java.time.Year;
    import java.time.YearMonth;
    
    public class LeapYear {
        public static void main(String[] args) {
            Year year = Year.of(2018); //用指定的年获取一个Year
               YearMonth yearMonth = year.atMonth(7); //从Year获取YearMonth
               LocalDate localDate = yearMonth.atDay(27); //YearMonth指定日得到LocalDate
               System.out.println("time = " + localDate);  //time = 2018-07-27
            //判断是否是闰年
               System.out.println("isLeapYear : " + localDate.isLeapYear()); //isLeapYear : false
            //自动处理闰年的2月日期
            //创建一个MonthDay
               MonthDay monthDay = MonthDay.of(2, 29);
            LocalDate localDate2 = monthDay.atYear(2012);  
            System.out.println("leapYear : " + localDate2);  //leapYear : 2012-02-29
            //同一个monthDay关联另一个年份上
               LocalDate nonLeapYear = monthDay.atYear(2014);
            System.out.println("isnotleapyear : " + nonLeapYear); //isnotleapyear : 2014-02-28
        }
    }
    package test;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    
    public class DateFormatDemo {
        public static void main(String[] args) {
            //获取当前时间
               LocalDate localDate = LocalDate.now();
            //指定格式规则
               DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
            //将当前时间格式化
               String str = localDate.format(formatter);
            System.out.println("时间 : " + str);      //时间 : 2018-07-27
        }
    }
    拼命敲
  • 相关阅读:
    第04组 Beta冲刺 (2/5)
    第04组 Beta冲刺 (1/6)
    第04组 Alpha冲刺 总结
    二叉树的递归与非递归
    各类典例模板
    选择题合辑2
    运算符重载
    链表题目
    集合的模拟实现(类模板)
    2018Final静态成员(黑名单)
  • 原文地址:https://www.cnblogs.com/wuyuwuyueping/p/9377925.html
Copyright © 2020-2023  润新知