获取当前时间日期的前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
- 获取当前时间日期的前n天日期
- javascript获取当前日期的前N天日期
- js获取当前日期的前后N天日期
- asp.net 获取 当前日期时间 及 前后N天日期时间(DropDownList数据绑定)
- asp.net 获取 当前日期时间 及 前后N天日期时间(DropDownList数据绑定)
- 根据当前日期得到前/后n天日期 根据指定日期得到前/后n天日期
- shell获取指定日期前(后)N天日期
- python获取指定日期的前n天或后n天日期
- android获取前n天日期、后n天日期
- js获取当前日期的前后N天日期的方法.html
- Java实现获取前、后N天日期的函数
- 获取前一月的当天日期时间
- (1)js 获取当前日期 前N天或者后N天日期 且可指定格式 如yyyy-mm-dd等
- 怎样算出当前日期的前7天日期啊?
- 当前之后n 天日期
- 获取当前月份第一天日期和最后一天日期
- Java实现获取前、后N天日期的函数分享
- Java实现获取前、后N天日期的函数分享2
- Redis 它是什么?它用来做什么?它的优势与短板如何?
- spring boot security默认配置启动导致的权限错误
- java继承、抽象、接口介绍以及区别
- 改背景
- 存储过程之二—变量
- 获取当前时间日期的前n天日期
- PHP中的数据类型
- pat甲级 10700---Mooncake
- 图片颜色转换
- appium生成测试报告的示例
- 文件处理命令rmdir
- SQL存储过程跨服务器访问
- android studio Execution failed for clean build失败 open editor
- 基于四元组的图像hash方法