java 获取本月最后一天,明天,昨天

package com.manji.finance.utils;

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

public class TimeUtils {


    //得到全时间
    public static String getFullTime() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);
        return curTime;
    }

    //精确到秒
    public static String getTimeUtilSecond() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);
        return curTime;

    }
    //精确到天
    public static String getTimeUtilDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);
        return curTime;

    }
    //纯数字
    public static String getPureTime() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);
        return curTime;

    }

    //当月第一天
    public static String getFirstDayInMonth(){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);
        curTime +="-01";


        return curTime;

    }
    //明天
    public static String getTomorrow(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long times = System.currentTimeMillis()+24*60*60*1000;
        Date d = new Date(times);
        String tomorrow = sdf.format(d);
        return tomorrow;

    }
    //昨天
        public static String getYesterday(){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            long times = System.currentTimeMillis()-24*60*60*1000;
            Date d = new Date(times);
            String yesteday = sdf.format(d);
            return yesteday;

        }
    //下个月第一天
    public static String getFirstDayNextMonth(){
        //当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);

        String[] t =curTime.split("-");

        String year =t[0];
        String month =t[1];

        if(month.equals(12)){
            year =Integer.valueOf(year)+1+"";
            month ="0"+1;
        }else if(Integer.valueOf(month)<9){
            month ="0"+(Integer.valueOf(month)+1)+"";
        }

        //下个月的第一天
        String nextMonthDay =year+"-"+month+"-"+"01 ";
        return nextMonthDay;
    }
    //当月最后一天
    public static String getLastDayInMonth(){
        //当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long times = System.currentTimeMillis();
        Date date = new Date(times);
        String curTime = sdf.format(date);

        String[] t =curTime.split("-");

        String year =t[0];
        String month =t[1];

        if(month.equals(12)){
            year =Integer.valueOf(year)+1+"";
            month ="0"+1;
        }else{
            month =Integer.valueOf(month)+1+"";
        }

        //下个月的第一天
        String tempTime =year+"-"+month+"-"+"01 ";
        String newTime ="";

        try {
            long nextDay =sdf.parse(tempTime).getTime();
            long lastDay =nextDay -24*60*60*1000;

            Date newDate = new Date(lastDay);


            newTime = sdf.format(newDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return newTime;

    }

    public static void main(String[] args){
        String name = getYesterday();
        System.out.println(name.split("-")[0]+name.split("-")[1]+name.split("-")[2]);

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值