0
点赞
收藏
分享

微信扫一扫

java-关于时间的API

爱情锦囊 2022-02-17 阅读 66

在JDK8之前的关于时间和日期的API
1,System中的currntTimeMillis( ) ;这个方法会返回一个从1970年1月0时0分0秒到现在的一个以毫秒为单位的时间差(也可以称为时间戳)

>public class DateTimeTest {
    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        System.out.println(time);//打印1644997960416
    }
}

2,使用java.util.Date类
Date的两个构造器
—空参构造器;可以创建一个当前时间的Date对象
—带参构造器;可以创建一个指定毫秒数的Date对象
Date中有很多方法都是已经不建议使用的过时方法,目前常用的两个方法有;toString();显示当前的时间到秒;getTime( );获取当前对象的对应毫秒数

import java.util.Date;

public class DateTimeTest {
    public static void main(String[] args) {
        //构造器1(当前时间的Date);
        Date date1 = new Date();
        String s = date1.toString();
        long time = date1.getTime();
        System.out.println(s);//Wed Feb 16 16:13:05 CST 2022
        System.out.println(time);//1644999185114
        //构造器2(指定毫秒数的Date);
        Date date2 = new Date(1644999185114l);
        System.out.println(date2);//Wed Feb 16 16:13:05 CST 2022
    }
}

3,java.util.Date与java.sql.Date的相互转换

事实上除了java.util下的Date还有一个同名字的Date在java.sql下,如果我们的代码中需要用到数据库,那么java.util下的Date是不适用的,我们需要的是java.sql下的Date,那么就需要我们将util下的Date转换为sql下的Date
而sql下的Date有一个带参构造器可以生成指定时间的Date,这样我们就可以实现相互转换了

import java.util.Date;

public class Sql_DateTest {
    public static void main(String[] args) {
        Date date = new Date();
        //创建一个sql下的Date并且将当前的时间作为参数传到构造器中
        java.sql.Date sd1 = new java.sql.Date(date.getTime());
        System.out.println(sd1);//2022-02-16
    }
}

4,Date的格式化与解析;
有些时候我们需要的是一个Date类来做相关的操作但是有些时候我们需要将Date转换为String类型的数据这时候就需要用到另一个类SimpleDateFormat();他有两个方法一个format方法一个parse方法
解析;
public String format(Date date);此方法更可以格式化时间对象Date
public Date parse(Sting source);此方法可以从给定的字符串开始解析文本以生成一个日期

SimpleDateFormat的解析步骤;
1,造一个SimpleDateFormat对象;
2,调用format方法并且将Date的实例化对象作为参数传到format方法中

public class Sql_DateTest {
    public static void main(String[] args){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        String format = simpleDateFormat.format(date);
        System.out.println(date);
        //格式化Date
        System.out.println(format);
        
        
    }
}

解析步骤
1,创建SimpleDateFormat对象
2,调用parse方法并且将需要解析的字符串当作参数传入parse方法内

注;传入的参数格式是有特殊要求的,默认的格式类似与上面解析出来的
“22-2-16 下午4:51”。格式也可以自己设置不同的格式(在源码中有不同的格式写法),只需要在创建SimpleDateFormat的时候使用带参数的构造器就行了

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

public class Sql_DateTest {
    public static void main(String[] args) throws  java.text.ParseException{
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        String format = simpleDateFormat.format(date);
        System.out.println(date);//Wed Feb 16 17:09:35 CST 2022
        //格式化Date
        System.out.println(format);//22-2-16 下午5:09
        //解析默认格式;
        Date date1 = simpleDateFormat.parse("22-2-16 下午4:51");//会抛一个ParseException异常
        System.out.println(date1);//Wed Feb 16 16:51:00 CST 2022
        //SimpleDateFormat带参数的构造器
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Date parse = simpleDateFormat1.parse("2022-12-4 08-12-55");
        System.out.println(parse);//Sun Dec 04 08:12:55 CST 2022
    }
}

SimpleDateFormat的其他参数格式;
在这里插入图片描述

5,使用Calendar类来操作时间
Calendar是一个抽象基类(不可以直接实例化)主要完成日期字段之间相互操作功能
实例化;
1,使用Calendar.getinstance( );方法
2,调用他的子类GregorianCalendar的构造器

calendar中的get,set,getTime,setTime方法

import java.util.Calendar;
import java.util.Date;

public class CalendarTest {
    public static void main(String[] args) {
        //实例化Calendar类
        Calendar calendar = Calendar.getInstance();
        //get方法,返回今天是这个月的第几天
        int i = calendar.get(calendar.DAY_OF_MONTH);
        //返回今天是今年的第几天
        int i1 = calendar.get(calendar.DAY_OF_YEAR);
        System.out.println(i);//17
        System.out.println(i1);//48

        //set方法;将今天重新设置为这个月的第五天
        calendar.set(calendar.DAY_OF_MONTH,5);
        i = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(i);//打印5

        //add方法;在原有的基础上将calendar加上五天并且返回
        calendar.add(calendar.DAY_OF_MONTH,5);
        i = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(i);//打印10

        //getTime方法;将calendar转换为Date
        Date time = calendar.getTime();
        System.out.println(time);//Thu Feb 10 12:52:19 CST 2022

        //setTime方法;将Date转换为calendar
        Date date = new Date();
        calendar.setTime(date);
        System.out.println(calendar.get(calendar.DAY_OF_MONTH));//17

    }
}

JDK8之后关于时间和日期的API

JDK8中新增的API

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class TimeTest
{
    public static void main(String[] args) {
        //实例化
        //获取当前的时间和日期
        LocalDateTime localDateTime = LocalDateTime.now();
        //获取当前的日期
        LocalDate localDate = LocalDate.now();
        //获取当前的时间
        LocalTime localTime = LocalTime.now();

        System.out.println(localDateTime);//2022-02-17T14:11:23.068
        System.out.println(localDate);//2022-02-17
        System.out.println(localTime);//14:11:23.068

        //of实例化,设置指定年月日时分秒的对象;
        LocalDateTime localDateTime1 = localDateTime.of(2022, 1, 10, 16, 15, 11);
        System.out.println(localDateTime1);//2022-01-10T16:15:11

        //getxxx的操作
        int dayOfMonth = localDateTime.getDayOfMonth();
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        int dayOfYear = localDateTime.getDayOfYear();
        System.out.println(dayOfMonth);//17
        System.out.println(dayOfWeek);//THURSDAY
        System.out.println(dayOfYear);//48

        //withxxx类似与calendar中的setxxx操作但是withxxx返回的是一个新的对象(不可变性)
        //将今天设置为这个月的第28号
        LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(28);
        System.out.println(localDateTime2);//2022-02-28T14:27:16.077
        //将今天设置为今年的第55天
        LocalDateTime localDateTime3 = localDateTime.withDayOfYear(55);
        System.out.println(localDateTime3);//2022-02-24T14:27:16.077

        //plus与minus  plus在原有的基础上将今天加;minus在原有的基础上减
        LocalDateTime localDateTime4 = localDateTime.plusDays(7);
        LocalDateTime localDateTime5 = localDateTime.minusDays(5);
        System.out.println(localDateTime4);//2022-02-24T14:30:01.778
        System.out.println(localDateTime5);//2022-02-12T14:30:01.778
    }
}
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class InstantTest {
    public static void main(String[] args) {
        //实例化Instant返回一个本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2022-02-17T07:41:47.500Z
        //因为获取的是本初子午线的时间所以当地时间需要加上偏移量八小时
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2022-02-17T15:43:49.807+08:00

        //toEpochMilli()方法;获取从1970年1月1日0时0分到现在的毫秒数
        long toEpochMilli = instant.toEpochMilli();
        System.out.println(toEpochMilli);//1645084030120

        //ofEpochMilli()方法;创建指定毫秒数的日期与时间
        Instant instant1 = Instant.ofEpochMilli(1645084030120l);
        System.out.println(instant1);//2022-02-17T07:47:10.120Z
    }
}

关于上面三种类的格式化;DateTimeFormatter的使用

方式一:预定义的标准格式。如:ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME

方式二:
本地化相关的格式,如ofLocalizedDateTime()
FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime

本地化相关的格式,如ofLocalizedDate()
FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate

重点:方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

public class DateTimeFormatterTest {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        //方式一:预定义的标准格式。如:ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化
        String format = dateTimeFormatter.format(localDateTime);
        System.out.println(format);//2022-02-17T22:09:26.4
        TemporalAccessor parse = dateTimeFormatter.parse("2022-02-17T22:17:45.671");
        System.out.println(parse);//{},ISO resolved to 2022-02-17T22:17:45.671


        //方式二:
        //本地化相关的格式,如ofLocalizedDateTime()
        //FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //格式化
        String format1 = formatter.format(localDateTime);
        System.out.println(format1);//22-2-17 下午10:17
        //解析
        TemporalAccessor parse1 = formatter.parse("22-2-17 下午10:20");
        System.out.println(parse1);

        //本地化相关的格式,如ofLocalizedDate()
        //FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format2 = dateTimeFormatter1.format(localDateTime);
        System.out.println(format2);//2022年2月17日 星期四

        //重点:方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format3 = dateTimeFormatter2.format(localDateTime);
        System.out.println(format3);//2022-02-17 10:29:23
    }
举报

相关推荐

0 条评论