three months timestamp

1、有效期三个月

package com.hengqin.life.idps;

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

/**
 * @author: ZhenGuangLi
 * @date: 2019/10/16
 * @Description:
 */
public class uuidTest {
    public static void main(String args[]) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse("2019-10-23 11:20:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -3);
        Date lastDate = calendar.getTime();
        System.out.println("三个月前的日期" + sdf.format(lastDate));
        calendar.setTime(lastDate);
        calendar.add(Calendar.MONTH, 3);
        Date forDate = calendar.getTime();
        System.out.println("三个月后的日期" + sdf.format(forDate));
        int pastTime = Integer.valueOf(String.valueOf(forDate.getTime() / 1000));
        System.out.println(pastTime);
    }
}

2、日期与时间戳的相互转换

    //日期转换成时间戳
    public   String dateToStamp(Date date){
        long it = date.getTime();
        return String.valueOf(it);
    }
    //时间戳转换成日期
    public  String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

猜你喜欢

转载自www.cnblogs.com/Small-sunshine/p/11725123.html