DateUtils工具类

//

// Source code recreated from a .class file by IntelliJ IDEA

// (powered by Fernflower decompiler)

//

package com.zuche.framework.utils;


import com.zuche.framework.utils.StringUtils;

import java.text.ParseException;

import java.text.ParsePosition;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class DateUtils {

public static final String NORMAL_FORMAT = "yyyy-MM-dd";

public static final String LONG_FORMAT = "yyyy-MM-dd HH:mm:ss";

private static final int FOUR = 4;

private static final int FIVE = 5;

private static final int SIX = 6;

private static final int SEVEN = 7;

private static final int EIGHT = 8;

private static final int NINE = 9;

private static final int TEN = 10;

private static final int TWELVE = 12;

private static final int THIRTEEN = 13;

private static final int FOURTEEN = 14;

private static final int TWENTY_FOUR = 24;

private static final int SIXTY = 60;

private static final int ONE_THOUSAND = 1000;

private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

public DateUtils() {

}

/** @deprecated */

@Deprecated

public static String getCurrentDateToCN(String dateTime) {

String year = dateTime.substring(0, 4);

String day = "";

String month;

if(dateTime.substring(5, 6).equals("0")) {

month = dateTime.substring(6, 7);

} else {

month = dateTime.substring(5, 7);

}

if(dateTime.substring(8, 9).equals("0")) {

day = dateTime.substring(9, 10);

} else {

day = dateTime.substring(8, 10);

}

return year + "年" + month + "月" + day + "日";

}

public static String convertDateStrToChineseDateStr(String dateTime, boolean zeroFlag) {

try {

if(dateTime.length() < 10) {

throw new IllegalArgumentException("日期字符串格式错误!");

} else {

String e = dateTime.substring(0, 4);

String month = dateTime.substring(5, 7);

String day = dateTime.substring(8, 10);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

String result = sdf.format(sdf.parse(e + "年" + month + "月" + day + "日"));

if(!zeroFlag) {

if(result.charAt(8) == 48) {

result = StringUtils.removeCharAt(result, Integer.valueOf(8));

}

if(result.charAt(5) == 48) {

result = StringUtils.removeCharAt(result, Integer.valueOf(5));

}

}

return result;

}

} catch (Exception var7) {

return null;

}

}

public static String convertChineseDateStrToDateStr(String dateTime, Character spliter) {

try {

if(dateTime.length() < 10) {

throw new IllegalArgumentException("中文日期字符串参数错误!");

} else {

SimpleDateFormat e = new SimpleDateFormat("yyyy年MM月dd日");

SimpleDateFormat resultFormat = new SimpleDateFormat("yyyy" + spliter.toString() + "MM" + spliter.toString() + "dd");

return resultFormat.format(e.parse(dateTime));

}

} catch (Exception var4) {

return null;

}

}

public static String convertChineseDateStrToDateStr(String dateTime) {

return convertChineseDateStrToDateStr(dateTime, Character.valueOf(‘-‘));

}

/** @deprecated */

@Deprecated

public static String getCurrentDate2Format() {

Calendar c = Calendar.getInstance();

return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);

}

public static String getCurrentDateStr() {

Calendar c = Calendar.getInstance();

return c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);

}

public static String getNMonthAfterCurrentDay(int n) {

Calendar c = Calendar.getInstance();

c.add(2, n);

return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);

}

public static String getNDayBeforeCurrentDate(int n) {

Calendar c = Calendar.getInstance();

c.add(5, -n);

return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);

}

public static String getNDayAfterCurrentDate(int n) {

Calendar c = Calendar.getInstance();

c.add(5, n);

return "" + c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5);

}

/** @deprecated */

@Deprecated

public static Calendar switchStringToCalendar(Date date) {

Calendar c = Calendar.getInstance();

c.setTime(date);

return c;

}

public static Calendar convertDateObjectToCalendar(Date date) {

Calendar c = Calendar.getInstance();

c.setTime(date);

return c;

}

public static int countDay(String startTime, String endTime) {

boolean day = false;

Calendar startTimec = Calendar.getInstance();

startTimec.setTime(dateString2Date(startTime));

Calendar endTimec = Calendar.getInstance();

endTimec.setTime(dateString2Date(endTime));

double countMillis = (double)((endTimec.getTimeInMillis() - startTimec.getTimeInMillis()) / 86400000L);

int day1 = (int)Math.ceil(countMillis);

return day1;

}

public static Date dateString2Date(String dateStr) {

return dateString2Date(dateStr, "yyyy-MM-dd");

}

public static Date dateString2Date(String dateStr, String partner) {

try {

SimpleDateFormat e = new SimpleDateFormat(partner);

ParsePosition pos = new ParsePosition(0);

Date current = e.parse(dateStr, pos);

return current;

} catch (NullPointerException var5) {

return null;

}

}

public static Calendar convertDateStrToCalendar(String dateStr) {

return convertDateStrToCalendar(dateStr, "yyyy-MM-dd");

}

public static Calendar convertDateStrToCalendar(String dateStr, String partner) {

try {

SimpleDateFormat e = new SimpleDateFormat(partner);

Date current = e.parse(dateStr);

Calendar c = Calendar.getInstance();

c.setTime(current);

return c;

} catch (Exception var5) {

return null;

}

}

public static int getYearOfDate(Date pDate) {

Calendar c = Calendar.getInstance();

c.setTime(pDate);

return c.get(1);

}

public static int getYearOfDate(String pDate) {

return getYearOfDate((Date)dateString2Date(pDate));

}

public static int getMonthOfDate(Date pDate) {

Calendar c = Calendar.getInstance();

c.setTime(pDate);

return c.get(2) + 1;

}

public static int getMonthOfDate(String date) {

return getMonthOfDate((Date)dateString2Date(date));

}

public static int getDayOfDate(Date pDate) {

Calendar c = Calendar.getInstance();

c.setTime(pDate);

return c.get(5);

}

public static int getHourOfDate(Date date) {

Calendar c = Calendar.getInstance();

c.setTime(date);

return c.get(11);

}

public static int getMinuteOfDate(Date pDate) {

Calendar c = Calendar.getInstance();

c.setTime(pDate);

return c.get(12);

}

public static int getSecondOfDate(Date pDate) {

Calendar c = Calendar.getInstance();

c.setTime(pDate);

return c.get(13);

}

public static long getMillisOfDate(Date pDate) {

Calendar c = Calendar.getInstance();

c.setTime(pDate);

return c.getTimeInMillis();

}

public static String toStrDateFromUtilDateByFormat(Date pUtilDate, String pFormat) throws ParseException {

String result = "";

if(pUtilDate != null) {

SimpleDateFormat sdf = new SimpleDateFormat(pFormat);

result = sdf.format(pUtilDate);

}

return result;

}

public String addDate(Date startDate, int count, int field, String format) throws ParseException {

int year = getYearOfDate((Date)startDate);

int month = getMonthOfDate((Date)startDate) - 1;

int day = getDayOfDate(startDate);

int hour = getHourOfDate(startDate);

int minute = getMinuteOfDate(startDate);

int second = getSecondOfDate(startDate);

GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute, second);

calendar.add(field, count);

return toStrDateFromUtilDateByFormat(calendar.getTime(), format);

}

public static boolean checkWeekendDay(String date) {

Calendar c = Calendar.getInstance();

c.setTime(dateString2Date(date));

int num = c.get(7);

return num == 6 || num == 7 || num == 1;

}

public static int[][] getMonthsByTime(String startTime, String endTime) {

ArrayList months = new ArrayList();

boolean m = false;

String[] start = startTime.split("-");

String[] end = endTime.split("-");

int year1 = StringUtils.parseInt(start[0]).intValue();

int year2 = StringUtils.parseInt(end[0]).intValue();

int month1 = StringUtils.parseInt(start[1]).intValue();

int month2 = StringUtils.parseInt(end[1]).intValue();

int count = year2 - year1;

int mon = 12;

if(count == 0) {

mon = month2;

}

while(month1 <= mon) {

months.add(year1 + "-" + month1++);

}

int j;

if(count > 1) {

for(int month = 1; month < count; ++month) {

++year1;

for(j = 1; j < 13; ++j) {

months.add(year1 + "-" + j);

}

}

}

if(count > 0) {

++year1;

month1 = 1;

while(month1 <= month2) {

months.add(year1 + "-" + month1++);

}

}

int var15 = months.size();

int[][] var16 = new int[var15][2];

for(j = 0; j < months.size(); ++j) {

String[] date = ((String)months.get(j)).split("-");

var16[j][0] = StringUtils.parseInt(date[0]).intValue();

var16[j][1] = StringUtils.parseInt(date[1]).intValue();

}

return var16;

}

public static int getDateDiff(DateUtils.DateType dateType, String pattern, boolean isRoundMode, String startDate, String... endDate) throws ParseException {

SimpleDateFormat fmt = new SimpleDateFormat(pattern);

Date startDate1 = fmt.parse(startDate);

Calendar starCal = Calendar.getInstance();

starCal.setTime(startDate1);

int sYear = starCal.get(1);

int sMonth = starCal.get(2);

int sDay = starCal.get(5);

Calendar endCal = Calendar.getInstance();

Date endDate1 = null;

int eMonth;

int eDay;

if(endDate.length != 0) {

if(endDate.length > 1) {

throw new RuntimeException("endDate参数只能是0个或者1个");

}

String[] eYear = endDate;

eMonth = endDate.length;

for(eDay = 0; eDay < eMonth; ++eDay) {

String date = eYear[eDay];

endDate1 = fmt.parse(date);

}

} else {

endDate1 = fmt.parse(getCurrentDateStr());

}

endCal.setTime(endDate1);

int var20 = endCal.get(1);

eMonth = endCal.get(2);

eDay = endCal.get(5);

switch(DateUtils.SyntheticClass_1.$SwitchMap$com$zuche$framework$utils$DateUtils$DateType[dateType.ordinal()]) {

case 1:

int year = var20 - sYear;

return year;

case 2:

int month = (var20 - sYear) * 12 + (eMonth - sMonth);

if(isRoundMode) {

month += eDay > sDay?1:0;

} else {

month += eDay >= sDay?0:-1;

}

return month;

case 3:

int day = (int)((endDate1.getTime() - startDate1.getTime()) / 86400000L);

return day;

default:

return 0;

}

}

public static Date dateAdd(Date date, String type, int value) {

Calendar c = Calendar.getInstance();

c.setTime(date);

if(!type.toLowerCase().equals("y") && !type.toLowerCase().equals("year")) {

if(!type.equals("M") && !type.toLowerCase().equals("month")) {

if(!type.toLowerCase().equals("d") && !type.toLowerCase().equals("date")) {

if(!type.toLowerCase().equals("h") && !type.toLowerCase().equals("hour")) {

if(!type.equals("m") && !type.toLowerCase().equals("minute")) {

if(type.toLowerCase().equals("s") || type.toLowerCase().equals("second")) {

c.add(13, value);

}

} else {

c.add(12, value);

}

} else {

c.add(10, value);

}

} else {

c.add(5, value);

}

} else {

c.add(2, value);

}

} else {

c.add(1, value);

}

return c.getTime();

}

public static int getWeekOfDate(String date) {

ParsePosition pos = new ParsePosition(0);

Date parseDate = (new SimpleDateFormat("yyyy-MM-dd")).parse(date, pos);

return parseDate.getDay();

}

public static String dateAddByString(String date, int amount) {

Date d = StringUtils.parseDate(date, "yyyy-MM-dd");

Calendar c = Calendar.getInstance();

c.setTime(d);

c.add(5, amount);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

return sdf.format(c.getTime());

}

public static String dateMonthAddByString(String date, int amount) {

Date d = StringUtils.parseDate(date, "yyyy-MM-dd");

Calendar c = Calendar.getInstance();

c.setTime(d);

c.add(2, amount);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

return sdf.format(c.getTime());

}

public static String formatDate(Date date) {

return formatDate((Date)date, "yyyy-MM-dd HH:mm:ss");

}

public static String formatDate(Date date, String format) {

return date == null?"":(new SimpleDateFormat(format)).format(date);

}

/** @deprecated */

@Deprecated

public static String formatDate(String date, String format) {

Date d = StringUtils.parseDate(date, format);

return d == null?"":(new SimpleDateFormat(format)).format(d);

}

public static String formatDateStr(String date, String format) {

Date d = StringUtils.parseDate(date, format);

return d == null?"":(new SimpleDateFormat(format)).format(d);

}

public static String getLastDayOfMonth(int year, int month) {

Calendar cal = Calendar.getInstance();

cal.set(1, year);

cal.set(2, month);

cal.set(5, cal.getActualMaximum(5));

return (new SimpleDateFormat("yyyy-MM-dd ")).format(cal.getTime());

}

public static String getLastDayOfMonth(String dateSt) {

int year = getYearOfDate((String)dateSt);

int month = getMonthOfDate((String)dateSt) - 1;

return getLastDayOfMonth(year, month);

}

public static long dateDiffToMinuteD(String startTime, String toTime) {

long diff = 0L;

if(StringUtils.checkEmpty(startTime) != null && StringUtils.checkEmpty(toTime) != null) {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

ParsePosition pos = new ParsePosition(0);

Date startTimeD = format.parse(startTime, pos);

pos.setIndex(0);

Date toTimeD = format.parse(toTime, pos);

diff = (startTimeD.getTime() - toTimeD.getTime()) / 1000L / 60L;

}

return diff;

}

public static String getNextDate(String thisDate, int days) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

Calendar cal = Calendar.getInstance();

try {

Date e = df.parse(thisDate);

cal.setTime(e);

} catch (ParseException var5) {

LOGGER.error(var5.getMessage(), var5);

throw new IllegalArgumentException("参数格式异常thisDate:" + thisDate);

}

cal.add(5, days);

return df.format(cal.getTime());

}

public static boolean isValidDate(String sDate) {

String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";

String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";

if(sDate != null) {

Pattern pattern = Pattern.compile(datePattern1);

Matcher match = pattern.matcher(sDate);

if(match.matches()) {

pattern = Pattern.compile(datePattern2);

match = pattern.matcher(sDate);

return match.matches();

} else {

return false;

}

} else {

return false;

}

}

public static String formatDate(String time) {

String ctime = null;

StringBuilder temp = new StringBuilder();

for(int timeFormat = 0; timeFormat < time.length(); ++timeFormat) {

if(time.charAt(timeFormat) >= 48 && time.charAt(timeFormat) <= 57) {

temp.append(time.charAt(timeFormat));

}

}

if(!StringUtils.isEmpty(temp.toString())) {

StringBuilder var4 = new StringBuilder();

if(temp.length() == 14) {

var4.append(temp.substring(0, 4));

var4.append("-");

var4.append(temp.substring(4, 6));

var4.append("-");

var4.append(temp.substring(6, 8));

var4.append(" ");

var4.append(temp.substring(8, 10));

var4.append(":");

var4.append(temp.substring(10, 12));

var4.append(":");

var4.append(temp.substring(12, 14));

} else if(temp.length() == 8) {

var4.append(temp.substring(0, 4));

var4.append("-");

var4.append(temp.substring(4, 6));

var4.append("-");

var4.append(temp.substring(6, 8));

var4.append(" 00:00:00");

}

ctime = var4.toString();

}

return ctime;

}

public static enum DateType {

year,

month,

day;

private DateType() {

}

}

}

时间: 2024-10-13 13:11:22

DateUtils工具类的相关文章

java日期的运用(DateUtils工具类)

public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("------当前时间--------:" + sd.format(now)); //年: 加.减操作 System.out.println("1年之后:&qu

Android经常使用工具类DateUtils(二)

在编写代码中,会经经常使用到时间Date这个类,小编整理了一些经常使用的时间工具类.供大家參考. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; /** * 日期工具类 */ public class DateUtils { /** * 获取格林威治时间(1

日期工具类 DateUtils(继承org.apache.commons.lang.time.DateUtils类)

/** * */ package com.dsj.gdbd.utils.web; import org.apache.commons.lang3.time.DateFormatUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; /** * 日期工具类, 继承org.apache.commons.l

实用篇:说说我在JavaScript项目中使用的工具类

在JavaScript的开发中,我们都会写一些工具类来帮我们简化一些业务操作的逻辑,一下就貼几个我在项目开发过程中常用的工具类.表达能力有限,各位看官还是看源码吧. 一.日期处理工具类. /** * 日期处理工具类 * @Authors: jackyWHJ * @date 2013-10-18 * */ var DateUtils = { /** * 得到日期在一年当中的周数 */ getISOYearWeek: function(date) { var commericalyear = thi

时间日期工具类

本工具类主要内容是LocalDateTime与Date的互转以及与日期字符串的相互转换,可与commons-lang-xxx.jar中提供的DateUtils配合使用 import java.time.*; import java.time.format.DateTimeFormatter; import java.util.Date; public class DateTimeUtils { /** * 格式化LocalDateTime实例为日期时间字符串 * * @param localDa

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ?Copyright 蕃薯耀 2017年9月13日 http://www.cnblogs.com/fanshuyao/ 直接上代码: import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.ref

springMVC的一些工具类

springMVC的一些工具类,主要有转换器,读取器 读取文件: package cn.edu.hbcf.common.springmvc; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.Confi

JAVA 常用的工具类总结

一.StringUtils类 import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; import net.sf.json.JSONObject; import sun.misc.BASE64Decoder; /**

代码编写中经常会涉及的工具类

这段时间,应着老板的要求,从无到有,从无从下手,到一步步摸索,终于顺利完成了老板交代的任务,在这次任务当中,学会了很多新知识,在这里,我将总结一下我在此次任务中经常用到的一些工具类,我认为这些工具类,有必要保存一份,这样,不至于到了编码的时候花更多时间去网上找,虽然我这也算在网上找的,但花了我不少时间,为了下次不再浪费更多时间,在此我将把一些常用的工具类分享出来. 我这次做的主要任务是,从网页抓取数据,数据量很大,抓取南方5省的天气数据,这当中涉及如何抓取数据,如何寻找可以被我们轻松解析的数据,