首页 > 学院 > 开发设计 > 正文

日期工具类

2019-11-09 15:04:15
字体:
来源:转载
供稿:网友
package com.lyt.base.util;import java.sql.Timestamp;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import com.lyt.base.util.DateModel;public class DateUtil {public static String datePattern = "MM/dd/yyyy";public static String timePattern = "HH:mm";public static String datePatternCommon = "yyyy-MM-dd";public static String datePatternAll = "yyyy-MM-dd HH:mm";/******* yyyy-MM-dd HH:mm:sss *********/public static String datePatternWithSecond = "yyyy-MM-dd HH:mm:ss";/******* yyyyMMddHHmmss *********/public static String datePatternWithSecond1 = "yyyyMMddHHmmss";public static String hourPattern = "HH";public static String minutePattern = "mm";/*** 获取当前日期时间(格式化到秒)* * @return*/public static String getDateTime() {String datestr = "";SimpleDateFormat df = new SimpleDateFormat(datePatternWithSecond);java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 格式化时间(时分秒)* * @return*/public static String getTime() {String datestr = "";DateFormat df = new SimpleDateFormat("HH:mm:ss");java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前年份* * @return*/public static String getYear() {String datestr = "";DateFormat df = new SimpleDateFormat("yyyy");java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前月份* * @return*/public static String getMonth() {String datestr = "";DateFormat df = new SimpleDateFormat("MM");java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取在当前月中的天数* * @return*/public static String getDay() {String datestr = "";DateFormat df = new SimpleDateFormat("dd");java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前小时数* * @return*/public static String getHour() {String datestr = "";DateFormat df = new SimpleDateFormat("HH");java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前分钟数* * @return*/public static String getMinute() {String datestr = "";DateFormat df = new SimpleDateFormat("mm");java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前日期* * @return*/public static String getDate() {String datestr = "";DateFormat df = new SimpleDateFormat(datePatternCommon);java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前日期精确到分钟* * @return*/public static String getDateShortTime() {String datestr = "";DateFormat df = new SimpleDateFormat(datePatternAll);java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}/*** 获取当前时间精确到秒* * @return*/public static String getDateTimeName() {String datestr = "";DateFormat df = new SimpleDateFormat(datePatternWithSecond);java.util.Date date = new java.util.Date();datestr = df.format(date);return datestr;}public static Date formatDateByString(String strDate) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = null;try {date = sdf.parse(strDate);} catch (ParseException e) {e.PRintStackTrace();}return date;}public static Date formatDateByString4(String strDate) {SimpleDateFormat sdf = new SimpleDateFormat(datePatternAll);Date date = null;try {date = sdf.parse(strDate);} catch (ParseException e) {e.printStackTrace();}return date;}public static Date formatDateByString2(String strDate) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date date = null;try {date = sdf.parse(strDate);} catch (ParseException e) {e.printStackTrace();}return date;}public static Date formatDateByString(String strDate, String format) {SimpleDateFormat sdf = new SimpleDateFormat(format);Date date = null;try {date = sdf.parse(strDate);} catch (ParseException e) {e.printStackTrace();}return date;}public static String formatStringByDate(Date date) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String strDate = sdf.format(date);return strDate;}public static String formatStringByDate(Date date, String format) {SimpleDateFormat sdf = new SimpleDateFormat(format);String strDate = sdf.format(date);return strDate;}public static String formatStringByString(String date) {String newDate = "";Date d = formatDateByString(date);newDate = formatStringByDate(d, "yyyyMMddHHmm");return newDate;}public static String formatStringByString(String date, String pattern) {String newDate = "";Date d = formatDateByString(date);newDate = formatStringByDate(d, pattern);return newDate;}public static long getDateLong(String date) {Date d = formatDateByString(date, "yyyy-MM-dd HH:mm:ss");return d.getTime();}public static String getStringByLong(long date) {Date d = new Date(date);String sDate = formatStringByDate(d, "yyyy-MM-dd HH:mm:ss");return sDate;}public static Date getDateByDate(Date date) {String sDate = formatStringByDate(date, "yyyy-MM-dd HH:mm:ss");Date sf = formatDateByString(sDate);return sf;}public static String getDayweek(Date date) {String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };Calendar cal = Calendar.getInstance();cal.setTime(date);int dayinweek = cal.get(Calendar.DAY_OF_WEEK) - 1;return weeks[dayinweek];}public static Date getWeekStart(Date date) {Calendar cal = Calendar.getInstance();cal.setTime(date);int day = cal.get(Calendar.DAY_OF_WEEK) - 1;cal.add(Calendar.DATE, -day + 1);return cal.getTime();}public static Date getWeekEnd(Date date) {Calendar cal = Calendar.getInstance();cal.setTime(date);int day = cal.get(Calendar.DAY_OF_WEEK) - 1;cal.add(Calendar.DATE, 6 - day + 1);return cal.getTime();}/*** 将指定的日期字符串按照指定的样式格式化成相应的日期型数据* * @param strDate*            日期字符串* @param strPattern*            格式化样式* @return 日期型数据实例或null[格式化错误]*/public static boolean validate(String strDate, String strPattern) throws Exception {if (strDate == null || "".equals(strDate)) {// throw new Exception("日期字符串为空:");return false;}if (strPattern == null || "".equals(strPattern)) {// throw new Exception("日期字符串的格式为空");return false;}if (datePatternCommon.equals(strPattern)) {if (!strDate.matches("^//d{4}//-//d{2}//-//d{2}$")) {// throw new Exception("日期字符串的格式与样式不符");return false;}} else if (datePatternAll.equals(strPattern)) {if (!strDate.matches("^//d{4}//-//d{2}//-//d{2}//s//d{2}//://d{2}$")) {// throw new Exception("日期字符串的格式与样式不符");return false;}}SimpleDateFormat format = new SimpleDateFormat(strPattern);format.setLenient(false);try {if (format.parse(strDate) != null)return true;elsereturn false;} catch (ParseException e) {// throw new Exception("日期字符串格式化失败");return false;}}/*** 将String转换成TimeStamp* * @param datetime* @return*/public static Timestamp getStringToTimestamp(String datetime) {Timestamp ts = new Timestamp(System.currentTimeMillis());try {ts = Timestamp.valueOf(datetime);} catch (Exception e) {e.printStackTrace();}return ts;}public static Timestamp formatStringDate(String date) {String[] str = date.split(",");StringBuffer str1 = new StringBuffer();for (int i = 0; i < str.length; i++) {if (str[i].length() == 1) {str[i] = "0" + str[i];}str1.append(str[i] + "-");}String str2 = str1.substring(0, str1.length() - 1);str2 = str2 + " 00:00:00";return Timestamp.valueOf(str2.toString());}public static Timestamp getTimeStampTime() {Date date = new Date();Timestamp nousedate = new Timestamp(date.getTime());return nousedate;}public static String getTimeStampTimeToString(Timestamp tamp) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String date = sdf.format(tamp);return date;}/*** 生成的时间加上10天为期限* * @param time* @return*/public static DateModel toYearAndMonthAndDay(Timestamp time, int day) {DateModel model = new DateModel();if (time.getDay() + 9 >= 20) {if (time.getMonth() == 12) {model.setYear(time.getYear() + 1 + 1900 + 1);model.setMonth(0);model.setDay(time.getDay() + 9 + day - 30);} else {model.setYear(time.getYear() + 1900);model.setMonth(time.getMonth() + 1);model.setDay(time.getDay() + 9 + day - 30);}} else {model.setYear(time.getYear() + 1900);model.setMonth(time.getMonth() + 1 - 1);model.setDay(time.getDay() + 9 + day);}return model;}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表