关于在Java里面,时间的一些业务

一、时间戳的比较

一般都用它来做登录判断,比如登录的时候检查该用户是否还在有效期范围内,如果不在开始时间与结束时间之内,那么就表明该用户已经过期;

这样的话,首先就可以想起三个参数,第一个需要获取当前系统时间,因为登录的时候需要根据当前时间来判断,

该用户是否还在有效期之内,然后再用数据库中获取该用户的开始时间与结束时间,这两个字段需要在数据库中存在;

1、首先,需要编写一个uti类,创建一个函数方法:

    /**
     * 时间格式化转换
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime()/1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 传入参数返回布尔类型,在此方法进行逻辑判断,如果当前时间(currenttime)在开始时间(startTime)与结束时间(endTime)之内就
     * 就返回true,否则就返回false
     * @param currenttime
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isUserChick(String currenttime,String startTime,String endTime){
       long  c= DateUtil.dateToTimestamp(currenttime);
        long s=dateToTimestamp(startTime);
        long e= DateUtil.dateToTimestamp(endTime);
        if (s<c&&c<e){
            return  true;
        }else {
            return  false;
        }
    }

2、在登录的拦截器中调用DateUtil中的isUserChick方法即可,需要传入三个参数,当前系统时间、开始时间、结束时间。

            /**
             *判断用户是否账号正常
             *登录判断当前时间是否在开始时间和结束时间范围内,如果不在提示账号过期
             */
                //获取当前系统时间
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
                String currenttime = dateFormat.format(date);
                //获取用户开始时间
                String startTime = user.getStartTime();
                //获取用户结束时间
                String endTime = user.getEndTime();
                //判断账号是否过期
                boolean flag=DateUtil.isUserChick(currenttime,startTime,endTime);
                if (flag==false){
                    throw new ExpiredCredentialsException();
                }

二、比较时间的一些方法

1、通过compareTo

Date date = new Date(1576118709574L);
Date date1 = new Date(1576118709574L);
Date date2 = new Date(1576118709575L);
System.out.println(date.compareTo(date2));//-1
System.out.println(date2.compareTo(date));//1
System.out.println(date.compareTo(date1));//0
前者大于后者 返回1
前者小于后者 返回-1
前者等于后者 返回0

2、通过before方法(不能比较等于)

Date date = new Date(1576118709574L);
Date date1 = new Date(1576118709574L);
Date date2 = new Date(1576118709575L);
System.out.println(date.before(date2));//true
System.out.println(date2.before(date));//false
System.out.println(date.before(date1));//false
前者在后者之前 返回true
前者在后者之后 返回false
前者等于后者   返回false

3、将日期转换为时间戳比较

Date date = new Date(1576118709574L);
Date date1 = new Date(1576118709574L);
Date date2 = new Date(1576118709575L);

System.out.println(date.getTime()>date1.getTime());

三、时间戳相加后的天数

package com.test;

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

public class Test1Time {
public static void main(String[] args){ int i=(int) (System.currentTimeMillis() / 1000); System.out.println(i); int a = 1; int b = 3; int j = i+a*60 * 60 * 24; int z = i+b*60 * 60 * 24; System.out.println("一天后的时间戳:"+j); System.out.println("三天后的时间戳:"+z); } }

四、时间戳与日期格式字符串的互转

package com.test;

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

public class DateUtil {
/** * 时间戳转换成日期格式字符串 * @param seconds 精确到秒的字符串 * @param formatStr * @return */ public static String timeStamp2Date(String seconds,String format) { if(seconds == null || seconds.isEmpty() || seconds.equals("null")){ return ""; } if(format == null || format.isEmpty()){ format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date(Long.valueOf(seconds+"000"))); }
/** * 日期格式字符串转换成时间戳 * @param date 字符串日期 * @param format 如:yyyy-MM-dd HH:mm:ss * @return */ public static String date2TimeStamp(String date_str,String format){ try { SimpleDateFormat sdf = new SimpleDateFormat(format); return String.valueOf(sdf.parse(date_str).getTime()/1000); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 取得当前时间戳(精确到秒) * @return */ public static String timeStamp(){ long time = System.currentTimeMillis(); String t = String.valueOf(time/1000); return t; } public static void main(String[] args) { String timeStamp = timeStamp(); System.out.println("timeStamp="+timeStamp); //运行输出:timeStamp=1470278082 System.out.println(System.currentTimeMillis());//运行输出:1470278082980 //该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数 String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss"); System.out.println("date="+date);//运行输出:date=2016-08-04 10:34:42 String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss"); System.out.println(timeStamp2); //运行输出:1470278082 } }
原文地址:https://www.cnblogs.com/ZJOE80/p/13723914.html