获取一天的开始时间和结束时间

本文介绍了一种使用Java编程语言获取指定日期的开始时间和结束时间的方法。通过SimpleDateFormat格式化当前日期,转换为字符串后再解析回Date类型,以此确定一天的起始时间。随后,通过计算毫秒数来确定当天的最后一秒,从而获得一天的结束时间。

获取一天的开始时间和结束时间

package com.bos.test;

import com.bos.common.file.FileThreadResource;
import org.junit.Test;

import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * https://www.cnblogs.com/metoy/p/4470418.html
 */
public class MenuTest {

    public static void main(String[] args) throws FileNotFoundException, ParseException {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String str = format.format(date);
        Date date2 = format.parse(str);
        System.out.println("开始时间----"+format2.format(date2));
        // 一天的毫秒-1
        int dayMis = 1000 * 60 * 60 * 24;
        // 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
        // 当天的毫秒
        long currentMillisecond = date2.getTime();
        // 当天最后一秒
        long resultMis = currentMillisecond + (dayMis - 1);
        // 得到我须要的时间 当天最后一秒
        Date resultDate = new Date(resultMis);
        System.out.println("结束时间----"+format2.format(resultDate));

    }


}

运行结果:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值