day_09_java高级编程_常用类_时间API_比较器(467~492)

时间戳:

public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。

import java.util.Date;
        Date d1 = new Date();
        System.out.println(d1.toString());//当前时间Wed Jan 05 11:34:05 CST 2022
        System.out.println(d1.getTime());//当前时间戳
        Date d1 = new Date();
        System.out.println(d1.toString());//当前时间Wed Jan 05 11:34:05 CST 2022
        System.out.println(d1.getTime());//当前时间戳
        Date d2 = new Date(d1.getTime());//将时间戳翻译为年、月、日、时、分、秒
        System.out.println(d2.toString());
        //将java中的data转换为sql中的data
        java.sql.Date d3 = new java.sql.Date(d1.getTime());//sql中的date 将时间戳翻译为2022-01-05 年、月、日
        System.out.println(d3);
              SimpleDateFormat sdf = new SimpleDateFormat();
        Date date = new Date();
        System.out.println(date);//Wed Jan 05 19:33:18 CST 2022
        System.out.println(sdf.format(date));//2022/1/5 下午7:33
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(sdf1.format(date));//22-01-05 07:33:18
        String s = sdf1.format(date);  //日期->字符串
        Date d2 = sdf1.parse(s);//字符串->日期  被转换的字符串格式必须和sdf1的格式相同yyyy-MM-dd hh:mm:ss 
        System.out.println(d2);//Wed Jan 05 07:43:14 CST 2022  

LocalDate、LocalTime、LocalDateTime的使用

import org.junit.Test;

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

/**
 * jdk 8中日期时间API的测试
 */
public class JDK8DateTimeTest {

    /**
     * LocalDate、LocalTime、LocalDateTime的使用
     *
     */
    @Test
    public void test1(){
        //now():获取当前的日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设置指定的年、月、日、时、分、秒。没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
        System.out.println(localDateTime1);

        //getXxx():获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        //体现不可变性
        //withXxx():设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);
        System.out.println(localDate1);

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        //不可变性
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);
    }
}

instent
import org.junit.Test;

import java.time.*;

/**
 * jdk 8中日期时间API的测试
 */
public class JDK8DateTimeTest {

    /**
     * Instant的使用
     */
    @Test
    public void test2(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);    //2020-05-10T09:55:55.561Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//东八区
        System.out.println(offsetDateTime); //2020-05-10T18:00:00.641+08:00

        //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数  ---> Date类的getTime()
        long milli = instant.toEpochMilli();
        System.out.println(milli);  //1589104867591

        //ofEpochMilli():通过给定的毫秒数,获取Instant实例  -->Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(1550475314878L);
        System.out.println(instant1);   //2019-02-18T07:35:14.878Z
    }
}

DateTimeFormatter的使用
import org.junit.Test;

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

/**
 * jdk 8中日期时间API的测试
 */
public class JDK8DateTimeTest {

    /**
     * DateTimeFormatter:格式化或解析日期、时间
     *     类似于SimpleDateFormat
     */
    @Test
    public void test3(){
        //方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str1 = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str1);//2020-05-10T18:26:40.234

        //解析:字符串 -->日期
        TemporalAccessor parse = formatter.parse("2020-05-10T18:26:40.234");
        System.out.println(parse);

        //方式二:
        //本地化相关的格式。如:ofLocalizedDateTime()
        //FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);//2020年5月10日 下午06时26分40秒

        //本地化相关的格式。如:ofLocalizedDate()
        //FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        //格式化
        String str3 = formatter2.format(LocalDate.now());
        System.out.println(str3);//2020-5-10


       //重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String str4 = formatter3.format(LocalDateTime.now());
        System.out.println(str4);//2020-05-10 06:26:40

        //解析
        TemporalAccessor accessor = formatter3.parse("2020-05-10 06:26:40");
        System.out.println(accessor);

    }
}

java比较器

一般的:java对象只能比较地址 ”== “ ” !=“
当我们需要比较两个对象的大小时可以采用接口的方式。

自然排序:java.lang.Comparable :实现Compareable接口:重写compareTo方法
定制排序:java.util.Comparator : 实现Compartor 接口 :重写 compare 方法

若自然排序与定制排序同时存在,则结果为定制排序。

实现Comparable接口 重写ComparaTo方法,实现自定义排序

  重写compareTo(obj)的规则:
  如果当前对象this大于形参对象obj,则返回正整数,
  如果当前对象this小于形参对象obj,则返回负整数,
  如果当前对象this等于形参对象obj,则返回零。
package domain;//package domain;

/**
 * @Classname goods
 * @Description TODO
 * @Date 2022/1/5 20:51
 * @Created by asus
 */

public class goods implements  Comparable{
    private  String name;
    private  double price;

    public goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    //实现Comparable接口 重写ComparaTo方法,实现自定义排序
    public int compareTo(Object o) {
        if (o instanceof  goods){
            goods g = (goods) o;
            if (this.price>g.price){
                return 1;
            }else if (this.price<g.price){
                return -1;
            }else{
                return  0;
            }
//            return Double.compare(this.price,g.price);

        }
        throw new RuntimeException("传入类型异常");
    }
}

Comparable接口与Comparator的使用的对比:
Comparable接口的方式一旦一定,保证Comparable接口实现类的对象在任何位置都可以比较大小。
Comparator接口属于临时性的比较。
在sort方法中new Comparator,然后临时重写方法

        Arrays.sort(gs, new Comparator<goods>() {
            @Override
            public int compare(goods o1, goods o2) {
                if (o1 instanceof  goods && o2 instanceof  goods){
                    goods g1 = (goods) o1;
                    goods g2 = (goods) o2;
                    if(g1.getName() == g2.getName()){
                        return 0;
                    }else{
                        return g1.getName().compareTo(g2.getName());
                    }
                }
                throw  new RuntimeException("类型错误");
            }

        });

一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值