java时间格式处理

一、根据当前时间,获取具体的时刻的时间

import java.text.SimpleDateFormat;
import java.util.Date;
 
public class getTime {
 
    public static void main(String[] args) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
        System.out.println(sd.format(time));
        System.out.println(sd.format(new Date()));
 
        //想获得N天之前或M天之后的时间
        int NDay = 5;
        int MDay = -10; //之后就传负数
 
        Date timeN = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * NDay);
        Date timeM = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * MDay);
 
        System.out.println(sd.format(timeN));
        System.out.println(sd.format(timeM));
 
        //想获得N小时之前或M小时之后的时间
        int NHour = 5;
        int MHour = -10; //之后就传负数
 
        Date timeNHour = new Date(new Date().getTime() - 60 * 60 * 1000 * NHour);
        Date timeMHour = new Date(new Date().getTime() - 60 * 60 * 1000 * MHour);
        System.out.println(sd.format(timeNHour));
        System.out.println(sd.format(timeMHour));
 
        //想获得N分钟之前或M分钟之后的时间
        int NMinute = 5;
        int MMinute = -10; //之后就传负数
 
        Date timeNMinute = new Date(new Date().getTime() - 60 * 1000 * NMinute);
        Date timeMMiute = new Date(new Date().getTime() - 60 * 1000 * MMinute);
        System.out.println(sd.format(timeNMinute));
        System.out.println(sd.format(timeMMiute));
 
        //想获得N秒之前或M秒之后的时间
        int NSecond = 5;
        int MSecond = -10; //之后就传负数
 
        Date timeNSecond = new Date(new Date().getTime() - 1000 * NSecond);
        Date timeMSecond = new Date(new Date().getTime() - 1000 * MSecond);
        System.out.println(sd.format(timeNSecond));
        System.out.println(sd.format(timeMSecond));
 
        long Ntime = getDetailtime(3, 20, 1, 1);
        System.out.println(sd.format(Ntime));
        System.out.println(Ntime);
 
        Date Ntime2 = getDetailDate(3, 20, 1, 1);
        System.out.println(sd.format(Ntime2));
        System.out.println(Ntime2);
 
    }
 
    public static Date getDetailDate(int Day, int Hour, int Minute, int Second) {
        Date timeN = new Date(new Date().getTime() - Day * 24 * 60 * 60 * 1000 - Hour * 60 * 60 * 1000 - Minute * 60 * 1000 - Second * 1000);
        return timeN;
    }
 
    public static long getDetailtime(int Day, int Hour, int Minute, int Second) {
        long timeN = new Date().getTime() - Day * 24 * 60 * 60 * 1000 - Hour * 60 * 60 * 1000 - Minute * 60 * 1000 - Second * 1000;
        return timeN;
    }
}

二、根据java.util.Calendar中的操作
ca.add(Calendar.DATE, -N);【减去N天】

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class getTime2 {
 
    public static void main(String[] args) {
 
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int N=2;
 
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -N);
        long timeDay= ca.getTime().getTime();
 
        Calendar ca2 = Calendar.getInstance();//得到一个Calendar的实例
        ca2.setTime(new Date()); //设置时间为当前时间
        ca2.add(Calendar.HOUR, -N);
        long timeDay2= ca2.getTime().getTime();
 
        Calendar ca3 = Calendar.getInstance();//得到一个Calendar的实例
        ca3.setTime(new Date()); //设置时间为当前时间
        ca3.add(Calendar.MINUTE, -N);
        long timeDay3= ca3.getTime().getTime();
 
        Calendar ca4 = Calendar.getInstance();//得到一个Calendar的实例
        ca4.setTime(new Date()); //设置时间为当前时间
        ca4.add(Calendar.SECOND, -N);
        long timeDay4= ca4.getTime().getTime();
 
        System.out.println(sd.format(timeDay));
        System.out.println(sd.format(timeDay2));
        System.out.println(sd.format(timeDay3));
        System.out.println(sd.format(timeDay4));
 
        Calendar ca5 = Calendar.getInstance();//得到一个Calendar的实例
        ca5.setTime(new Date()); //设置时间为当前时间
        ca5.add(Calendar.MONTH, -N);
        long timeDay5= ca5.getTime().getTime();
        System.out.println(sd.format(timeDay5));
 
        Calendar ca6 = Calendar.getInstance();//得到一个Calendar的实例
        ca6.setTime(new Date()); //设置时间为当前时间
        ca6.add(Calendar.YEAR, -N);
        long timeDay6= ca6.getTime().getTime();
        System.out.println(sd.format(timeDay6));
 
    }
}

三、Java中Date 有一些不建议用的方法
在这里插入图片描述
四、运用已有的一些jar依赖
org.apache.commons.lang3.time.DateUtils;

import org.apache.commons.lang3.time.DateUtils;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class TestLongtime {
 
    public static void main(String[] args) {
 
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        System.out.println(sd.format(new Date()));
        System.out.println(sd.format(new Date().getTime()));
 
 
        Date now = new Date();
        Date year   = DateUtils.addYears(now, -2);
        Date day    = DateUtils.addDays(now, -2);
        Date hour   = DateUtils.addHours(now, -2);
        Date minute = DateUtils.addMinutes(now, -2);
        Date second = DateUtils.addSeconds(now, -2);
 
        //long bb = Long.parseLong(startDate.toString());
 
        System.out.println(sd.format(year));
        System.out.println(sd.format(day));
        System.out.println(sd.format(hour));
        System.out.println(sd.format(minute));
        System.out.println(sd.format(second));
 
    }
}

5、常用的String类型转换到long类型
long bb = Long.parseLong(startDate.toString());

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值