java实战 时间处理

Last updated on September 15, 2024 pm

🧙 Questions

timestamp转date

Instant instant = Instant.parse(String.valueOf(componentValue));
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.ofHours(8));
return String.valueOf(Timestamp.valueOf(localDateTime).getTime());

获取当前分钟数小时数

package com.ispong.demo;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

public class Demo {

    @Test
    public void demo() {

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

        // 分钟数
        System.out.println("分钟数:" + now.getMinute());

        // 小时数
        System.out.println("小时数:" + now.getHour());
    }

}

计算两个日期之间小时数

package com.ispong.demo;

import org.junit.jupiter.api.Test;

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

public class Demo {

    @Test
    public void demo() throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String beforeDateStr = "2020-11-01 10:38:00";
        String afterDateStr = "2020-11-01 09:54:58";

        Date beforeDate = simpleDateFormat.parse(beforeDateStr);
        Date afterDate = simpleDateFormat.parse(afterDateStr);

        long nh = 1000L * 60 * 60;

        double h = (double) (beforeDate.getTime() - afterDate.getTime()) / nh;
        h = (double) Math.round(h * 10) / 10;
        System.out.println("between two date hour is" + h);
    }

}

计算两个时间的间隔月数

package com.ispong.demo;

import org.junit.jupiter.api.Test;

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

public class Demo {

    @Test
    public void demo() throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String beforeDateStr = "2020-11-01 10:38:00";
        String afterDateStr = "2020-12-01 09:54:58";

        Date beforeDate = simpleDateFormat.parse(beforeDateStr);
        Date afterDate = simpleDateFormat.parse(afterDateStr);

        Calendar firstCalender = Calendar.getInstance();
        firstCalender.setTime(beforeDate);

        Calendar secondCalender = Calendar.getInstance();
        secondCalender.setTime(afterDate);

        int firstYear = firstCalender.get(Calendar.YEAR);
        int secondYear = secondCalender.get(Calendar.YEAR);
        int firstMonth = firstCalender.get(Calendar.MONTH);
        int secondMonth = secondCalender.get(Calendar.MONTH);

        if (firstYear == secondYear) {
            System.out.println("between month:" + Math.abs(firstMonth - secondMonth));
        } else if (secondYear > firstYear) {
            System.out.println("between month:" + (12 - firstMonth + secondMonth + (secondYear - firstYear - 1) * 12));
        } else {
            System.out.println("between month:" + (12 - secondMonth + firstMonth + (firstYear - secondYear - 1) * 12));
        }
    }

}

计算两个时间的间隔天数

package com.ispong.demo;

import org.junit.jupiter.api.Test;

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

public class Demo {

    @Test
    public void demo() throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String beforeDateStr = "2020-11-01 10:38:00";
        String afterDateStr = "2020-12-01 09:54:58";

        Date beforeDate = simpleDateFormat.parse(beforeDateStr);
        Date afterDate = simpleDateFormat.parse(afterDateStr);

        int days = new BigDecimal(String.valueOf(Math.abs((beforeDate.getTime() - afterDate.getTime()) / 1000 / 60 / 60 / 24))).setScale(1, BigDecimal.ROUND_DOWN).intValue();
        System.out.println("between day:" + days);
    }

}

java实战 时间处理
https://ispong.isxcode.com/spring/java/java实战 时间处理/
Author
ispong
Posted on
January 13, 2021
Licensed under