java常用类--时间日期类

Date 类

package cn.usts.edu.fly.DateAndDateFormat;

import com.sun.org.apache.xerces.internal.impl.dv.xs.YearMonthDV;

import java.time.*;

/**
 * @author :fly
 * @description: Date 类
 *                    在jdk8之前还没有time包,使用Date类,用Calendar.getInstance()获取时间对象
 *                    java.time提供了更多对时间日期的操作
 * @date :2021/10/31 16:44
 */
public class DateDemo {
    
    
    public static void main(String[] args) {
    
    
        LocalTime time = LocalTime.now();
        System.out.println(time);// 时间 16:51:42.206

        LocalDate date = LocalDate.now();
        System.out.println(date);//当前年月日 2021-10-31

        LocalDate date1 = date.plusDays(1);
        System.out.println("当前时间加1天是"+date1);

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        System.out.println("当前年"+Year.now());
        System.out.println("当前年月"+ YearMonth.now());//2021-10
        System.out.println("当前月日"+ MonthDay.now());// --10-31
    }
}

SimpleDateFormat(推荐)

package cn.usts.edu.fly.DateAndDateFormat;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author :fly
 * @description:  SimpleDateFormat()  自定义日期时间格式
 * @date :2021/10/31 17:08
 */
public class SimpleDateFormatDemo {
    
    
    public static void main(String[] args) {
    
    
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("现在的时间是:yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(new Date()));// 今天是:2021-10-31 17:11:47

    }
}

Guess you like

Origin blog.csdn.net/qq_43619461/article/details/121066491