Java常用类(日期类,Calendar、Date、LocalDateTime)-10

Date

package com.edu.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo01 {
    public static void main(String[] args) {
        //获取当前时间
        Date d1 = new Date();
        System.out.println(d1); //外国格式
        //创建SimpleDateFormat,可以指定格式,看jdk文档指定格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = sdf.format(d1);
        System.out.println(format);

        //字符串转日期
        String s = "2023年12月09日 10:31:33 星期六";
        try {
            Date parse = sdf.parse(s);
            System.out.println(sdf.format(parse));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


    }
}
练习:两个时间差
package com.edu.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Demo02 {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        try {
            Date d1 = sdf.parse("2004年01月05日 13:31:40");
            Date d2 = sdf.parse("2004年01月02日 13:30:40");
            long diff =d1.getTime()-d2.getTime();
            long days = diff/(1000*60*60*24);
            long hours = (diff-days*(1000*60*60*24))/(1000*60*60);
            long minues = (diff-days*(1000*60*60*24)-hours*(1000*60*60))/(1000*60);
            System.out.println("结果:\t"+days+"天"+hours+"小时"+minues+"分");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //获取当前日期前半个小时的时间和后半个小时的时间
        Date now = new Date();
        System.out.println("当前时间:"+sdf.format(now.getTime()));

        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE,30);
        Calendar nowTime2 = Calendar.getInstance();
        nowTime2.add(Calendar.MINUTE,-30);
        System.out.println("30分钟后时间:"+sdf.format(nowTime.getTime()));
        System.out.println("30分钟前时间:"+sdf.format(nowTime2.getTime()));

        //指定时间的10分钟之后
        Calendar nowTime3 = Calendar.getInstance();
        try {
            nowTime3.setTime(sdf.parse("2020年06月11日 12:00:00"));
            nowTime3.add(Calendar.MINUTE,-10);
            System.out.println("10分钟之后:"+sdf.format(nowTime3.getTime()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }
}

Calendar

package com.edu.date;

import java.util.Calendar;

public class Demo03 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        System.out.println(c);
        //获取日历对象的某个字段,自由组合字段输出
        //如果小时要换成24小时进制的话 ==>Calendar.Hour ---> Calendar.Hour_Or_Day
        System.out.println("年:"+c.get(Calendar.YEAR));
        System.out.println("月:"+(c.get(Calendar.MONTH)+1));//老外喜欢从0开始
        System.out.println("日:"+c.get(Calendar.DAY_OF_MONTH));



    }
}

 第三代日期类

Java常用类(日期类,Calendar、Date、LocalDateTime)-10_第1张图片

package com.edu.date;

import java.sql.Date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Demo04 {
    public static void main(String[] args) {
        //1、使用now() 返回表示当前时间的对象
        LocalDateTime nowTime = LocalDateTime.now();//LocalDate.now(),LocalTime.now()
        System.out.println(nowTime);
        System.out.println("年"+nowTime.getYear());
        System.out.println("月"+nowTime.getMonth());
        System.out.println("月"+nowTime.getMonthValue());
        System.out.println(nowTime.getDayOfYear());
        //2、格式化
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss");
        String format = dateFormat.format(nowTime);
        System.out.println(format);
        //3、时间戳
        //1 通过now()获取当前时间戳对象
        Instant now =Instant.now();
        //2 通过from将Instant转换为Date
       // Date date = (Date) Date.from(now);
        //3 转回时间戳
       // Instant instant = date.toInstant();
        //对时间进行加或者减
        LocalDateTime nowTime1 = nowTime.plusDays(890);
        System.out.println(dateFormat.format(nowTime1));
    }

}
package com.edu.date;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Demo06 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime end = LocalDateTime.of(2023,12,12,22,8,11);
        Duration dur = Duration.between(now,end);
        System.out.println(dur.toDays());
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"); //hh为12小时,HH为24小时
        String dateFormat = format.format(end);
        System.out.println(dateFormat);
    }
}

Java常用类(日期类,Calendar、Date、LocalDateTime)-10_第2张图片

你可能感兴趣的:(Java入门学习,java,开发语言)