<span style="font-size:18px;"><span style="font-size:18px;">/**
* 字符串转换成Date类型
* @param strDate
* @return
*/
public static Date getDateFromString(String strDate) {
<span style="white-space:pre"> </span>if (StringUtils.isBlank(strDate)) {
<span style="white-space:pre"> </span>return new Date();
<span style="white-space:pre"> </span>}
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simple.parse(strDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}</span></span>
新建一个DateUtil.java类文件
<span style="font-size:18px;">package com.ffkj.education.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static Date getActionEndTime(Date date, int minute) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, minute);
date = c.getTime();
return date;
}
public static Date getReportEndDay(Date date) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
String start = f.format(date) + "-01";
return parseDate(start);
}
public static Date parseDate(String start) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = df.parse(start);
return date;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public static Calendar getCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
public static Date addtDay(Date date, int number) {
Calendar c = getCalendar(date);
c.add(Calendar.DAY_OF_MONTH, number);
return c.getTime();
}
public static Date getLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
return calendar.getTime();
}
public static Date getLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
return calendar.getTime();
}
public static Date nextYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, 1);
date = c.getTime();
return date;
}
public static Date getNowDateShort() throws ParseException {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return formatter.parse(dateString);
}
public static Date getDateFromStringByYYMMDD(String strDate){
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
try {
date= simple.parse(strDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
public static Date getNextDay(Date date) {
Calendar c = getCalendar(date);
c.add(Calendar.DAY_OF_MONTH, 1);
return c.getTime();
}
public static Date getPreDay(Date date) {
Calendar c = getCalendar(date);
c.add(Calendar.DAY_OF_MONTH, -1);
return c.getTime();
}
/**
* 字符串转换成Date类型 格式为:【yyyy-MM-dd】
* @param strDate
* @return
*/
public static Date getDateFromString(String strDate) {
if (StringUtils.isBlank(strDate)) {
return new Date();
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simple.parse(strDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
* 字符串转换成Date类型 格式为:【yyyy/MM/dd】
* @param strDate
* @return
*/
public static Date getDateFromStringBySqlDate(String strDate) {
if (StringUtils.isBlank(strDate)) {
return new Date();
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd");
Date date = null;
try {
date = simple.parse(strDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
public static Date getDateFromStringBySqlDateYYYYMMDDHHMMSS(String strDate) {
if (StringUtils.isBlank(strDate)) {
return new Date();
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date date = null;
try {
date = simple.parse(strDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
public static String getStringDateYYYYMMDD(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
String newDate = simple.format(date);
return newDate;
}
public static String getDateFromStringYYYYMMDD(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
String newDate = simple.format(date);
return newDate;
}
public static String getDateFromStringYYYYMMDDHHMM(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newDate = simple.format(date);
return newDate;
}
public static String getDateFromString(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newDate = simple.format(date);
return newDate;
}
public static String getDateFromStringYYYMMDD(Date date){
SimpleDateFormat simple = new SimpleDateFormat ("yyyy-MM-dd");
String newDate = simple.format(date);
return newDate;
}
public static Date getDateFromDate(Date date) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
String newString = simple.format(date);
Date newdate = null;
try {
newdate = simple.parse(newString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newdate;
}
public static String cal(int second) {
int h = 0;
int d = 0;
int s = 0;
int temp = second % 3600;
if (second > 3600) {
h = second / 3600;
if (temp != 0) {
if (temp > 60) {
d = temp / 60;
if (temp % 60 != 0) {
s = temp % 60;
}
} else {
s = temp;
}
}
} else {
d = second / 60;
if (second % 60 != 0) {
s = second % 60;
}
}
return h + "\u65F6" + d + "\u5206" + s + "\u79D2";
}
public static String getDate(Date date) {
long miaoshu = date.getTime();
return String.valueOf(miaoshu);
}
// a integer to xx:xx:xx
public static String secToTime(int time) {
String timeStr = null;
int hour = 0;
int minute = 0;
int second = 0;
if (time <= 0)
return "00:00:00";
else {
minute = time / 60;
if (minute < 60) {
second = time % 60;
timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
} else {
hour = minute / 60;
if (hour > 99)
return "99:59:59";
minute = minute % 60;
second = time - hour * 3600 - minute * 60;
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
}
}
return timeStr;
}
public static String unitFormat(int i) {
String retStr = null;
if (i >= 0 && i < 10)
retStr = "0" + Integer.toString(i);
else
retStr = "" + i;
return retStr;
}
}
</span>