|
@@ -9,73 +9,112 @@ import java.util.List;
|
|
|
|
|
|
public class DateUtils {
|
|
|
|
|
|
- public static Integer getDateInteger(String date) {
|
|
|
- if (!Check.isNull(date)) {
|
|
|
- String replace = date.replace("-", "");
|
|
|
- return Integer.valueOf(replace);
|
|
|
+ public static Integer getDateInteger(String date) {
|
|
|
+ if (!Check.isNull(date)) {
|
|
|
+ String replace = date.replace("-", "");
|
|
|
+ return Integer.valueOf(replace);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public static String getNowDate(String format) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
- Date date = new Date();
|
|
|
- return sdf.format(date);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getAnotherDay(String format, String date, Integer num) {
|
|
|
-
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
- Date getDate = null;
|
|
|
- try {
|
|
|
- getDate = sdf.parse(date);
|
|
|
- } catch (ParseException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ public static Long getDateLong(String date) {
|
|
|
+ if (!Check.isNull(date)) {
|
|
|
+ String replace = date.replace("-", "");
|
|
|
+ return Long.valueOf(replace);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(getDate);
|
|
|
- calendar.add(Calendar.DAY_OF_MONTH, num);
|
|
|
- Date resultDate = calendar.getTime();
|
|
|
- return sdf.format(resultDate);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取 两个日期之间的所有 日期
|
|
|
- * @param startDate
|
|
|
- * @param endDate
|
|
|
- */
|
|
|
- public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- List<String> dateList = new ArrayList<String>();
|
|
|
- try {
|
|
|
- Date dateOne = sdf.parse(startDate);
|
|
|
- Date dateTwo = sdf.parse(endDate);
|
|
|
-
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(dateOne);
|
|
|
-
|
|
|
- dateList.add(startDate);
|
|
|
- while (calendar.getTime().before(dateTwo)) { //倒序时间,顺序after改before其他相应的改动。
|
|
|
- calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
- dateList.add(sdf.format(calendar.getTime()));
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ public static String getNowDate(String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ Date date = new Date();
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getAnotherDay(String format, String date, Integer num) {
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ Date getDate = null;
|
|
|
+ try {
|
|
|
+ getDate = sdf.parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(getDate);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, num);
|
|
|
+ Date resultDate = calendar.getTime();
|
|
|
+ return sdf.format(resultDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取 两个日期之间的所有 日期
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ */
|
|
|
+ public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ List<String> dateList = new ArrayList<String>();
|
|
|
+ try {
|
|
|
+ Date dateOne = sdf.parse(startDate);
|
|
|
+ Date dateTwo = sdf.parse(endDate);
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(dateOne);
|
|
|
+
|
|
|
+ dateList.add(startDate);
|
|
|
+ while (calendar.getTime().before(dateTwo)) { //倒序时间,顺序after改before其他相应的改动。
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ dateList.add(sdf.format(calendar.getTime()));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dateList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date addDay(Date date, int day) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_YEAR, day);
|
|
|
+ return calendar.getTime();
|
|
|
}
|
|
|
- return dateList;
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addDay(Date date, int day) {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(date);
|
|
|
- calendar.add(Calendar.DAY_OF_YEAR, day);
|
|
|
- return calendar.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static Date getNowDate() throws ParseException {
|
|
|
- SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- String nowDate = getNowDate("yyyy-MM-dd");
|
|
|
- return dsdf.parse(nowDate);
|
|
|
- }
|
|
|
|
|
|
+ public static Date getNowDate() throws ParseException {
|
|
|
+ SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String nowDate = getNowDate("yyyy-MM-dd");
|
|
|
+ return dsdf.parse(nowDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 日期转换为时间戳(毫秒)
|
|
|
+ */
|
|
|
+ public static long timeToStamp(String timers) {
|
|
|
+ Date d = new Date();
|
|
|
+ long timeStemp = 0;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ d = sf.parse(timers);// 日期转换为时间戳
|
|
|
+ } catch (ParseException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ timeStemp = d.getTime();
|
|
|
+ return timeStemp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*时间戳(毫秒)转换为日期
|
|
|
+ format:yyyyMMdd 、 yyyy-MM-dd
|
|
|
+ */
|
|
|
+ public static String tempToDate(long millis, String format) {
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ Date d1 = new Date(millis);
|
|
|
+ String d2 = simpleDateFormat.format(d1);
|
|
|
+ return d2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ System.out.println(tempToDate(1704858060000L,"yyyy-MMdd"));
|
|
|
+ }
|
|
|
}
|