• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Dixon_Liang

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

日期类

package com.changyonglei.Data;

import org.junit.Test;

import java.util.Date;

/**
 * @author Dixon
 * @create 2022-05-25 15:07
 *
 * JDK 8 之前日期和时间的API测试
 *
 *
 * 1. java.lang.System类  ----test1()
 * System类提供的public static long currentTimeMillis()
 *用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
 * 此方法适于计算时间差。
 * ●计算世界时间的主要标准有:
 * UTC(Coordinated Universal Time)
 * GMT(Greenwich Mean Time)
 * CST(Central Standard Time)
 *
 *
 * 2.java.util.Date类 ----test2()
 * 表示特定的瞬间,精确到毫秒构造器:
 * Date():使用无参构造器创建的对象可以获取本地当前时间。Date(long date)
 * 常用方法
 * getTime():返回自1970年1月1日00:00:00 GMT以来此 Date对象表示的毫秒数。
 * toString():把此 Date对象转换为以下形式的 String: dow mon dd
 * hh:mm:ss zzz yyyy其中: dow是一周中的某一天(Sun,Mon,Tue,Wed,Thu, Fri,Sat),zzz是时间标准。
 */

public class DateTimeTest {

    //1.system类中的currentTimeMillis()
    @Test
    public void test1() {
        long time = System.currentTimeMillis();
        //返回当前时间与1970年1月1日0时0分0秒之间以亳秒为单位的时间差。

        System.out.println(time);// 称为时间戳
    }

    @Test
    public void test2() {
        /***
         * java.util.Date类
         *      /---java.sql.Date类
         *
         * 1.java.util.Date类的两个构造器的使用
         *      构造器一:Date():创建一个对应当前时间的Date对象
         *      构造器二:创建指定毫秒数的Date对象
         * 2.java.util.Date类的两个方法的使用
         *          toString()∶显示当前的年、月、日、时、分、秒
         *          getTime():获取当前Date对象对应的毫秒数。(时间歌)
         *
         *
         * 3.java.sql.Date类 对应着数据库中的日期类型的变量
         * >如何实例化
         * >如何将java.util.Date对象转换为java.sal.Date对象
         */

        //构造器一:Date():创建一个对应当前时间的Date对象
        Date date1 = new Date();
        System.out.println(date1.toString());//Wed May 25 15:20:37 CST 2022
        System.out.println(date1.getTime()); //1653463379811 时间戳

        //构造器二:创建指定毫秒数的Date对象
        Date date2 = new Date(1653463379811L);
        System.out.println(date2);  // Wed May 25 15:22:59 CST 2022

        //创建sql.Date对象
        java.sql.Date date3 = new java.sql.Date(1653463379811L);
        System.out.println(date3.toString()); // 2022-05-25

        //如何将java.util.Date对象转换为java.sqL.Date对象
        //情况一:
//        Date date4 =new java.sql.Date(1653463379811L);
//        java.sql.Date date5 = (java.sql.Date)date4;

        // 情况二:
        Date  date6 =new Date();
        java.sql.Date date7 = new java.sql.Date(date6.getTime());
    }


}

 

package com.changyonglei.Data;

import org.junit.Test;

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

/**
 * jdk 8 之前的日期时间API测试
 *
 * 1.System类中currenTimeMillis()
 * 2.java.util.Date 和子类java.sql.Date
 * 3.SimpleDateFormat
 * 4.Calendar
 *
 * @author Dixon
 * @create 2022-05-28 17:50
 */
public class DateTimeTest2 {

    /**
     *
     * SimpleDateFormat的使用: SimpleDateFormat对日期Date类的格式化和解析
     * 1.两个操作:
     * 1.1格式化:日期--->字符串
     * 1.2解析:格式化的逆过程,字符串--->日期
     *
     * 2.SimpleDateFormat的实例化
     *
     */
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //实例化 SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat();
        //格式化:日期-->字符串
        Date date = new Date();
        System.out.println(date); //Sat May 28 21:12:52 CST 2022
        String format = sdf.format(date);
        System.out.println(format); // 22-5-28 下午9:13

        //解析:格式化的逆过程,字符串--->日期
        String str ="22-5-28 下午9:13";
        Date date1 = sdf.parse(str);
        System.out.println(date1);

        System.out.println("-----按照指定方式格式化,调用带参数的构造器----------");
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MMMM.dd GGG hh:mm aaa");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        // 格式化Date
        String format1 = sdf1.format(date);
        System.out.println(format1);
        // 解析: 要求字符串必须SimpleDateFormat识别的格式,否则抛异常
        Date date2= sdf1.parse("2022-05-28 09:43:03");
        System.out.println(date2);
    }

    /**
     * 练习一:字符串"2020-09-08”转换为java.sql.Date
     */
    @Test //练习一
    public void test1() throws ParseException { //字符串"2020-09-08”转换为java.sql.Date
        String str = "2020-09-08";
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = s1.parse(str);
        System.out.println(date1);

        Long s2 = date1.getTime();
        java.sql.Date date = new java.sql.Date(s2);

        System.out.println(date);

    }

    /**
     * 练习二:"三天打渔两天晒网” 1990-01-01    XXXX-XX-xx打渔?晒网? 举例:2020-09-08
     *
     * 总天数% 5 == 1,2,3 :打渔
     * 总天数% 5 == 4,0 :晒网
     */
    @Test //练习二
    public void test2() throws ParseException {
        String str1 ="1990-01-01";
        String str2 ="2020-09-08";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd");

        Date d1 = sdf1.parse(str1);
        Date d2 = sdf1.parse(str2);

        Long l1 = d1.getTime();
        Long l2 = d2.getTime();
        Long l3 = (l2-l1) / (1000 * 60 * 60 * 24) + 1; //毫秒换成天
//        System.out.println(l3);

        if (l3 % 5 ==1 || l3 % 5 ==2 || l3 % 5 ==3){
            System.out.println("打渔");
        }else if(l3 % 5 ==0 || l3 % 5 ==4){
            System.out.println("晒网");
        }

    }
}

 

package com.changyonglei.Data;

import org.junit.Test;

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

/**
 *
 * java.Calendar 日历类的使用
 *
 * @author Dixon
 * @create 2022-05-29 10:47
 */
public class DateTimeTest3 {

    @Test
    public void test1(){
        //1.实例化
        //方式一:创建其子类(GregorianCalendar)的对象
        // /方式二:调用其静态方法getInstance()
        Calendar calendar = Calendar.getInstance();
//        System.out.println(calendar.getClass()); // class java.util.GregorianCalendar 还是返回它子类

        // 2.常用方法
        //get()
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));

        //set()
        calendar.set(Calendar.DAY_OF_MONTH,22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //add()
        calendar.add(Calendar.DAY_OF_MONTH,3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime() 日历类---》Date
        Date date = calendar.getTime();
        System.out.println(date);

        //setTime
        Date date1 = new Date();
        calendar.setTime(date1);
        System.out.println(date1);

        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }
}

 jdk8以后都是用这个类:

package com.changyonglei.Data;

import org.junit.Test;

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

/**
 *
 *
 *localDate  LocalTime LocalDateTime的使用
 * 说明:
 *
 *      1.LocalDateTime的使用相比LocalTime、localDate,使用频率要高
 *      2.类似于Calendar
 *
 * @author Dixon
 * @create 2022-05-29 15:08
 */
public class DateTimeTest4 {

    @Test

    public void test1(){
        // now():获取当前的日期 时间 日期+时间
        LocalDate localdate = LocalDate.now();
        LocalTime localtime = LocalTime.now();
        LocalDateTime localdatetime = LocalDateTime.now();

        System.out.println(localdate); // 2022-05-29
        System.out.println(localtime);  //21:05:48.903
        System.out.println(localdatetime); //2022-05-29T21:05:48.903

        //of(): 设置指定的年、月、日、时、分、秒 没有偏移量
        LocalDateTime localdatetime1 = LocalDateTime.of(2022, 5, 29, 23, 59, 59,44);
        System.out.println(localdatetime1);//2022-05-29T23:59:59.000000044

        //getXxx()
        System.out.println(localdatetime.getDayOfMonth()); //29
        System.out.println(localdatetime.getDayOfWeek()); //SUNDAY
        System.out.println(localdatetime.getMonth()); //MAY
        System.out.println(localdatetime.getMinute()); //5

        //withXxx() 设置相关属性 ,体现不可变性
        LocalDate localdate1 = localdate.withDayOfMonth(22); //改成22号
        System.out.println(localdate); // 2022-05-29
        System.out.println(localdate1); //2022-05-22

        //plusXxx()
        LocalDateTime localDateTime2 = localdatetime.plusMonths(2); // 加2个月
        System.out.println(localdatetime); //2022-05-29T21:18:40.124
        System.out.println(localDateTime2); //2022-07-29T21:18:40.124

        //minusXxx()
        LocalDateTime localDateTime3 = localdatetime.minusDays(5); //减5天
        System.out.println(localdatetime); //2022-05-29T21:20:08.947
        System.out.println(localDateTime3); // 2022-05-24T21:20:08.947
    }
}

 

package com.changyonglei.Data;

import org.junit.Test;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;

/**
 * @author Dixon
 * @create 2022-05-29 21:26
 */
public class DateTimeTest5 {

    @Test
    public void test1(){
        Instant now = Instant.now(); // 本初子午线的标准时间
        System.out.println(now); //2022-05-29T13:27:34.936Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2022-05-29T21:30:02.558+08:00

        //获取对应的毫秒数l获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 =时间戳 ==Date类的getTime()
        long milli = now.toEpochMilli();
        System.out.println(milli);

        Date date = new Date();
        long time = date.getTime();
        System.out.println(time);

        //ofEpochMilli 时间戳换算成时间
        Instant instant = Instant.ofEpochMilli(1653831277100L);
        System.out.println(instant); //2022-05-29T13:34:37.100Z
    }
}

 

package com.changyonglei.Data;

import org.junit.Test;

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

/**
 * DateTimeFormatter:格式化或解析日期、时间
 * 类似于SimpLeDateFormat
 *
 * @author Dixon
 * @create 2022-05-29 21:38
 */
public class DateTimeTest6 {

    @Test
    public void test1(){

        //方式一: 预定义的标准格式。如: ISO_LOCAL_DATE_TIME;  ISO_LOCAL_DATE;  ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime now = LocalDateTime.now();
        final String format = formatter.format(now);
        System.out.println(now); //2022-05-29T22:14:05.860
        System.out.println(format); //2022-05-29T22:14:05.86

        //解析:字符串--> 日期
        TemporalAccessor parse = formatter.parse("2022-05-29T22:14:05.86");
        System.out.println(parse); //{},ISO resolved to 2022-05-29T22:14:05.860


        // 方式二:本地化相关的格式。如: ofLocalizedDateTime()
        // FormatStyLe.LONG / FormatStyLe.MEDIUM / FormatStyLe.SHORT:适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //格式化:
        String format1 = formatter1.format(now);
        System.out.println(format1); //22-5-29 下午10:23
        //解析同上-->方式一

        //本地化相关的格式。如: ofLocalizedDate()
        //FormatStyLe.FULL / FormatStyle.LONG / FormatStyLe.MEDIUM/ FormatStyLe.SHORT︰适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        LocalDate now1 = LocalDate.now();
        //格式化:
        String format2 = formatter2.format(now1);
        System.out.println(now1); //2022-05-29
        System.out.println(format2); //2022年5月29日 星期日
        //解析同上-->方式一

        //方式三:自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss E ")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format3 = formatter3.format(LocalDateTime.now());
        System.out.println(format3); //2022-05-29 10:33:56
        //解析
        TemporalAccessor parse1 = formatter3.parse("2022-05-29 10:33:56");
        System.out.println(parse1);//{NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=33, HourOfAmPm=10, SecondOfMinute=56, MicroOfSecond=0},ISO resolved to 2022-05-29



    }
}

 

posted on 2022-05-25 15:44  Dixon_Liang  阅读(42)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3