java 关于时间

/**
 * 是否连续签到(true连续、false未连续),今天和昨天比较 判断yesterday 是否是昨天的时间,true表示是昨天
 *
 * @param uid
 * @return
 */
public boolean ifSignedContinuous(Long uid){
    boolean flag = false;
    try{
        User user = appUserMapper.selectByPrimaryKey(uid);
        if(user != null){
            if(user.getSignLastTime() != null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String todayStr  = sdf.format(new Date()).toString();
                Date today = sdf.parse(todayStr);
                String yesterdayStr = sdf.format(user.getSignLastTime()).toString();
                Date yesterday = sdf.parse(yesterdayStr);
                if((today.getTime()-yesterday.getTime())>0 && (today.getTime()-yesterday.getTime())<=86400000) {
                    return true;
                }
            }
        }
    }catch (Exception e){
    }
    return flag;
}

获取上个月

String dateTime=‘2021-10’;

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");        

Date date = sf.parse(dateTime);        

Calendar calendar = Calendar.getInstance();        

calendar.setTime(date);        

calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月        

date = calendar.getTime();        

String accDate = sf.format(date);         //accDate

/**
* 判断1个时间 是否是今天 
*/
for(Obj bean :list){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(new Date()).toString();
    String createTime = sdf.format(bean.getCreateTime()).toString();
    if(today.equals(createTime)){
        flag = true;
        break;
    }
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String time = df.format(new Date()).toString();
System.out.println("AAAAA==================  "+time);

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String time = df.format(new Date()).toString();
log.setId(Long.parseLong(time));

//获取当前时间 往前推移的某一个时间点
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();

calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -1);
Date date = calendar.getTime();
String month = format.format(date);
System.out.println("1个月前:"+month);

calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -3);
Date date3 = calendar.getTime();
String month3 = format.format(date3);
System.out.println("3个月前:"+month3);

calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -6);
Date date6 = calendar.getTime();
String month6 = format.format(date6);
System.out.println("6个月前:"+month6);

calendar.setTime(new Date());
calendar.add(Calendar.YEAR, -1);
Date y = calendar.getTime();
String year = format.format(y);
System.out.println("1年前:"+year);

//linux时间戳 
long currentTime = Calendar.getInstance().getTimeInMillis();
System.out.println("AAAAABB==================  "+currentTime/1000);

String转date
String txTime = ((Object)txsObject.get("txTime")).toString();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date chainTime = formatter.parse(txTime, pos);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值