⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untitled1.java

📁 获得上一天、上一周、上一月、上一季、上一年的时间。
💻 JAVA
字号:
package untitled3;

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

public class Untitled1 {
    /**
     * 同比增长
     *
     * @param old
     * @param news
     * @return
     */
    public int epexegesis(int old, int news) {
        if (old == 0 || (news - old) == 0) {
            return 0;
        }
        System.out.println((news - old) / (old+0.0));
        return (news - old) % old;
    }

    public static void main(String args[]){
        SimpleDateFormat fm = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
        Untitled1 u = new Untitled1();
//        u.epexegesis(8,5);
//        System.out.println(fm.format(new Date(System.currentTimeMillis())));
//        System.out.println(fm.format(u.getUpDay(System.currentTimeMillis())));
        Date date[];
        date = u.getAllDate();
        for(int i=0;i<date.length;i++){
            System.out.println(date[i]);
        }

    }
    /**
     * 获得上天、周、月、季、年时间
     * @return Date[] 0天、1周、2月、3季、4年
     */
    public Date[] getAllDate(){
        Date date[] = new Date[5];
        date[0] = getUpDay(System.currentTimeMillis());
        date[1] = getUpWeek(System.currentTimeMillis());
        date[2] = getUpMonth(System.currentTimeMillis());
        date[3] = getUpQuarter(System.currentTimeMillis());
        date[4] = getUpYear(System.currentTimeMillis());
        return date;
    }
//    public
    /**
     * 取得上一天
     * @param curDate long 当前时间
     * @return Date 昨天时间
     */
    public Date getUpDay(long curDate){
        Date upWeekTime = new Date(curDate-(24*60*60*1000));
        return upWeekTime;
    }
    /**
     * 取得上周
     * @param curDate long 当前时间
     * @return Date 上周时间
     */
    public Date getUpWeek(long curDate){
        Date upWeekTime = new Date(curDate-(7*24*60*60*1000));
        return upWeekTime;
    }
    /**
     * 取得上月
     * @param curDate long 当前时间
     * @return Date 上个月时间
     */
    public Date getUpMonth(long curDate){
        Date date = new Date(curDate);
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date))-1;
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
        int h = Integer.parseInt(new SimpleDateFormat("HH").format(date));
        int m = Integer.parseInt(new SimpleDateFormat("mm").format(date));
        int s = Integer.parseInt(new SimpleDateFormat("ss").format(date));
        //当月份为1时上一个月应该是年减1月份为12
        if(month==0){
            year=year-1;
            month=12;
        }
        Calendar c = Calendar.getInstance();
        c.set(year,month-1,1,h,m,s);
        int tmp = c.getActualMaximum(c.DAY_OF_MONTH);
        if(day>tmp)
            day = tmp;
        c.set(year,month-1,day,h,m,s);//Calendar类的月份从0开始所以另减一个1
        c.set(year,month-1,day,h,m,s);
        return c.getTime();
    }
    /**
     * 取得上季度
     * @param curDate long 当前时间
     * @return Date 上季度时间
     */
    public Date getUpQuarter(long curDate) {
        Date date = new Date(curDate);
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) -3;
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
        int h = Integer.parseInt(new SimpleDateFormat("HH").format(date));
        int m = Integer.parseInt(new SimpleDateFormat("mm").format(date));
        int s = Integer.parseInt(new SimpleDateFormat("ss").format(date));
        //当月份为1时上一个月应该是年减1月份为12
        if (month == 0) {
            year = year - 1;
            month = 12;
        }
        else if(month == -1){
            year = year - 1;
            month = 11;
        }
        else if(month == -2){
            year = year - 1;
            month = 10;
        }
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, 1, h, m, s);
        int tmp = c.getActualMaximum(c.DAY_OF_MONTH);
        if (day > tmp)
            day = tmp;
        c.set(year, month - 1, day, h, m, s); //Calendar类的月份从0开始所以另减一个1
        c.set(year, month - 1, day, h, m, s);
        return c.getTime();
    }
    /**
     * 取得上年时间
     * @param curDate long 当前时间
     * @return Date 上一年时间
     */
    public Date getUpYear(long curDate){
        Date date = new Date(curDate);
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
        int h = Integer.parseInt(new SimpleDateFormat("HH").format(date));
        int m = Integer.parseInt(new SimpleDateFormat("mm").format(date));
        int s = Integer.parseInt(new SimpleDateFormat("ss").format(date));
        year=year-1;

        Calendar c = Calendar.getInstance();
        c.set(year,month-1,1,h,m,s);
        int tmp = c.getActualMaximum(c.DAY_OF_MONTH);
        if(day>tmp)
            day = tmp;
        c.set(year,month-1,day,h,m,s);
        return c.getTime();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -