java获取昨天今天明天的日期

 

 我刚写完这个代码,就被开除了........

 

 代码双手奉上以备后用:

package mydemo;

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

public class ThreeDay {
    public static void main(String[] args) {
        getToday();
        getYesterday();
        getTomorrow();
    }

    /**
     * 获取今天的日期
     */
    public static void getToday() {
        //SimpleDateFormat smdate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date = smdate.format(new Date());
        System.out.println(date);
    }

    /**
     * 获取昨天的日期
     */
    public static void getYesterday() {
        SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        String date = smdate.format(calendar.getTime());
        System.out.println(date);
    }

    /**
     * 获取明天的日期
     */
    public static void getTomorrow() {
        SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, 1);
        String date = smdate.format(calendar.getTime());
        System.out.println(date);
    }

}

 

结果:

 

 

补充:

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DATE);
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
package util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class Time {

    /**
     * 获取指定月份
     * 
     * @param months
     * @return
     */
    public static List<String> getMonthsBetween(int months) {
        List<String> listMonth = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        for (int i = 1; i <= months; i++) {
            c.setTime(new Date());
            c.add(Calendar.MONTH, -i);
            Date m = c.getTime();
            listMonth.add(sdf.format(m));
        }
        Collections.reverse(listMonth);
        return listMonth;
    }

    /**
     * 获取前几天的日期
     * 
     * @param days
     * @return
     */
    public static List<String> getDaysBetwwen(int days) {
        Calendar calendar = Calendar.getInstance();
        List<String> listDays = new ArrayList<>();
        // 1天
        Long oneDay = 1000 * 60 * 60 * 24l;
        for (int i = 0; i < days; i++) {
            Date d = new Date(calendar.getTimeInMillis() - oneDay * i);
            DateFormat df = new SimpleDateFormat("MM月dd日");
            // System.out.println(df.format(d));
            listDays.add(df.format(d));
        }
        Collections.reverse(listDays); // 倒序排列
        return listDays;
    }

    public static String getLast12Months(int i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -i);
        Date m = c.getTime();
        return sdf.format(m);
    }

    public static void main(String[] args) {
        List<String> daysBetwwen = getDaysBetwwen(7);
        System.out.println(daysBetwwen.toString());
        List<String> monthsBetween = getMonthsBetween(7);
        // Collections.sort(monthsBetween); // 顺序排列
        // Collections.shuffle(monthsBetween); // 混乱的意思
        // Collections.binarySearch(monthsBetween, " a5 ");
        // Collections.reverse(monthsBetween);// 倒序排列
        System.out.println(monthsBetween.toString());
    }

}

 

posted @ 2019-12-14 00:48  图图小淘气_real  阅读(2923)  评论(0编辑  收藏  举报