获取当前时间日期的前n天日期

来源:互联网 发布:折800淘宝 编辑:程序博客网 时间:2024/06/06 05:21
效果图效果图package yunup.com.yaomeanagernew.tool;import java.text.SimpleDateFormat;import java.util.Calendar;/** * Created by user on 2017/10/11. */public class MyDateTimeUtils {    public static final int DAY = 1;    public static final int WEEK = 2;    public static final int MOTH = 3;    /**     * @param type   要返回的类型  (日,周,月)     * @param number 返回的数量(例如近4天)     * @param fromat 返回的日期的格式(yyyy-MM-dd HH:mm:ss:ms)     * @return     */    public static String[] getLatelyDate(int type, int number, String fromat) {        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fromat);        Calendar calendar = Calendar.getInstance();        long currentTime = calendar.getTimeInMillis();        String[] strings = new String[number];        switch (type) {            case DAY:                String currentDay = simpleDateFormat.format(calendar.getTimeInMillis());//当前的日期                strings[number - 1] = currentDay;                for (int j = 0; j < number - 1; j++) {                    calendar.add(Calendar.DAY_OF_MONTH, -1);//前一天的日期                    strings[number - 1 - j - 1] = simpleDateFormat.format(calendar.getTimeInMillis());                }                return strings;            case WEEK:                int getcurrentWeek = calendar.get(Calendar.WEEK_OF_MONTH);//获取当前是周几                int currentWeek = 1;//这个才是真实的周几                if (getcurrentWeek - 1 == 0) {                    currentWeek = 7;                } else {                    currentWeek = getcurrentWeek - 1;                }                String week1 = simpleDateFormat.format(calendar.getTimeInMillis());//当前的日期                strings[number - 1] = week1;                calendar.add(Calendar.DAY_OF_MONTH, -currentWeek);//上周 周末 的日期 时间                String week2 = simpleDateFormat.format(calendar.getTimeInMillis());//当前的日期                strings[number - 2] = week2;                for (int j = 0; j < number - 2; j++) {                    calendar.add(Calendar.DAY_OF_MONTH, -7);//出了本周和上周,其他的一律减去7天就是   上周的周末                    strings[number - 2 - j - 1] = simpleDateFormat.format(calendar.getTimeInMillis());                }                return strings;            case MOTH:                int currentMonth = calendar.get(Calendar.MONTH);//获取当前月份                int currentYear = calendar.get(Calendar.YEAR);//获取当前年份//                strings[number - 1] = currentYear + "年" + currentMonth + "月";                strings[number - 1] = simpleDateFormat.format(calendar.getTimeInMillis());                for (int j = 0; j < number - 1; j++) {                    currentMonth--;                    if (currentMonth == 0) {                        currentMonth = 12;                        currentYear--;                    }                    calendar.set(currentYear,currentMonth,calendar.get(Calendar.DAY_OF_MONTH));//                    strings[number - 1 - j - 1] = currentYear + "年" + currentMonth + "月";                    strings[number - 1 - j - 1] = simpleDateFormat.format(calendar.getTimeInMillis());                }                return strings;        }        return null;    }}
阅读全文
0 0