vo中对date格式的时间格式的处理,⽅便到前台展⽰
之前⼀个同事的写法是这样的(绿⾊的):
public static final String GENERAL_PATTERN_2 = "yyyy-MM-dd HH:mm:ss";
其实没必要这样,在vo⾥⾯的set⽅法做个赋值:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
这样不⾏,因为这⾥的createtime是string的,不能这样写(如果createtime属性是Date类型,给String类型的createtimeStr 属性赋值可以这样写,createtimeStr这个字段值常⽤于前端展⽰~ 不⽤再在前端处理),写了访问报500错:
那就这样写:
public String getCreatetime() {
try {
StringDate(DateUtils.stringToDate(createtime, DateUtil.GENERAL_PATTERN_2));
} catch (ParseException e) {
e.printStackTrace();
}
return createtime;
}
重要⼯具类DateUtils.java具体代码如下
import java.math.BigDecimal;
ParseException;
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtils {
private static final int BIN_DIGITS = 12;
public static final short ONE_MS = 1 << BIN_DIGITS;
public static String getDatePattern() {
return "yyyy-MM-dd";
}
public static String getTimePattern() {
return "yyyy-MM-dd HH:mm:ss";
}
public static String getDatePattern8() {
return "yyyyMMdd";
}
java时间日期格式转换public static String getTimePattern14() {
return "yyyyMMddHHmmss";
}
public static Date getDateTime(Date date, String format) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(format);
String strDate = df.format(date);
Calendar cal = new GregorianCalendar();
cal.setTime(stringToDate(strDate, format));
Time();
}
public static int getYear() {
Instance().get(Calendar.YEAR);
}
public static int getYear(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
(Calendar.YEAR);
}
public static int getMonth() {
Instance().get(Calendar.MONTH) + 1;
}
public static int getMonth(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
(Calendar.MONTH) + 1;
}
public static int getDayOfMonth() {
Instance().get(Calendar.DAY_OF_MONTH);
}
public static int getDayOfMonth(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
(Calendar.DAY_OF_MONTH);
}
public static final Date stringToDate(String strDate, String format) throws ParseException { SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(format);
try {
date = df.parse(strDate);
} catch (ParseException pe) {
throw new Message(), pe.getErrorOffset());
}
return (date);
}
public static Date stringToDate(String strDate) throws ParseException {
Date aDate = null;
try {
aDate = stringToDate(strDate, getDatePattern());
} catch (ParseException pe) {
pe.printStackTrace();
throw new Message(), pe.getErrorOffset());
}
return aDate;
}
public static final String dateToString(Date aDate, String format) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
//
} else {
df = new SimpleDateFormat(format);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String getStringDate(Date date) {
SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM⽉dd⽇ HH时mm分ss秒");
return format1.Time());
}
public static final String dateToString(Date aDate) {
return dateToString(aDate, getDatePattern());
}
public static final String timeToString(Date aDate) {
return dateToString(aDate, getTimePattern());
}
public static Date getDate(Date aDate) throws ParseException {
return getDateTime(aDate, getDatePattern());
}
public static Date getTime(Date aDate) throws ParseException {
return getDateTime(aDate, getTimePattern());
}
public static long getDaysBetween(Date dtFrom, Date dtEnd) {
long begin = Time();
long end = Time();
long inter = end - begin;
if (inter < 0) {
inter = inter * (-1);
}
long dateMillSec = 24 * 60 * 60 * 1000;
long dateCnt = inter / dateMillSec;
long remainder = inter % dateMillSec;
if (remainder != 0) {
dateCnt++;
}
return dateCnt;
}
public static long getMinsBetween(Date dtFrom, Date dtEnd) {
long begin = Time();
long end = Time();
long inter = end - begin;
return inter / 60000;
}
public static String getLastDateOfyearAndMonth(String yearAndMonth) throws Exception {
String start = yearAndMonth + "01";
Date startDate = DateUtils.stringToDate(start, "yyyyMMdd");
Calendar calendar = Instance();
calendar.setTime(startDate);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
String end = DateUtils.Time(), "yyyyMMdd");
return end;
}
public static String getNextMonthString(String strDate, String format) throws ParseException { Date date = stringToDate(strDate, format);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.MONTH, 1);
return Time(), format);
}
/**
* 获取上个⽉的最后⼀天
*
* @param date
* @return
* @throws ParseException
*/
public static Date getLastDayOfLastMonth(Date date) throws ParseException {
String str_firstDay = dateToString(date, "yyyyMM") + "01";
Date date_firstDay = stringToDate(str_firstDay, "yyyyMMdd");
if (date_firstDay != null) {
Calendar cal = Instance();
cal.setTime(date_firstDay);
cal.add(Calendar.DAY_OF_MONTH, -1);
Time();
}
return null;
}
/**
* 获取上个⽉的最后⼀天
*
* @param date
* @return
* @throws ParseException
*/
public static String getLastDayOfLastMonth(Date date, String format) throws ParseException { return dateToString(getLastDayOfLastMonth(date), format);
}
/**
* 获取传⼊时间所在⽉的最后⼀天
*
* @param date
* @return
*/
public static int getLastDay(Date date) {
Calendar calendar = Instance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
(Calendar.DAY_OF_MONTH);
}
/**
* 获取指定时间下个⽉的第⼀天零时00:00:00
*
* @param date
* @return
*/
public static Date getNextMonthFirstDay(Date date) {
Calendar calendar = Instance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Time();
}
public static Date getMonthLastDay(Date date) {
Calendar calendar = Instance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
Time();
}
/**
* 获取⼀个⽉的开始时间
*
* @param date
* @return
*/
public static Date getMonthFirstDay(Date date) {
Calendar calendar = Instance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Time();
}
/**
* 当前时间是否在抽奖的有效时间内
*
* @return
*/
public static boolean raffleEffectiveTime() {
boolean effective = false;
try {
Date begin = DateUtils.stringToDate("20130701000000", "yyyyMMddHHmmss");
Date end = DateUtils.stringToDate("20131001000000", "yyyyMMddHHmmss");
Date current = new Date();
if (currentpareTo(begin) == 1 && currentpareTo(end) == -1) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return effective;
}
/**
* 将源格式字符串⽇期转换成⽬标格式字符串⽇期
*
* @param origStrDate 字符串⽇期
* @param origFormat 源格式
* @param destFormat ⽬标格式
* @return
* @throws ParseException
*/
public static final String dateStrToString(String origStrDate, String origFormat, String destFormat) throws ParseException { Date origDate = stringToDate(origStrDate, origFormat);
return dateToString(origDate, destFormat);
}
// 季度⼀年四季,第⼀季度:2⽉-4⽉,第⼆季度:5⽉-7⽉,第三季度:8⽉-10⽉,第四季度:11⽉-1⽉
private static int getQuarterInMonth(int month, boolean isQuarterStart) {
int months[] = { 1, 4, 7, 10 };
if (!isQuarterStart) {
months = new int[] { 3, 6, 9, 12 };
}
if (month >= 2 && month <= 4)
return months[0];
else if (month >= 5 && month <= 7)
return months[1];
else if (month >= 8 && month <= 10)
return months[2];
else
return months[3];
}
/
/ 获得本周⼀与当前⽇期相差的天数
public static int getMondayPlus() {
Calendar cd = Instance();
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) {
return -6;
} else {
return 2 - dayOfWeek;
}
}
// 获得当前周- 周⼀的⽇期
public static Date getCurrentMonday() {
Calendar cal = Instance();
cal.setTime(getThisWeekMonday(new Date()));
cal.add(Calendar.DATE, -7);
Time();
}
public static Date getThisWeekMonday(Date date) {
Calendar cal = Instance();
cal.setTime(date);
// 获得当前⽇期是⼀个星期的第⼏天
int dayWeek = (Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置⼀个星期的第⼀天,按中国的习惯⼀个星期的第⼀天是星期⼀
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前⽇期是⼀个星期的第⼏天
int day = (Calendar.DAY_OF_WEEK);
// 根据⽇历的规则,给当前⽇期减去星期⼏与⼀个星期第⼀天的差值
cal.add(Calendar.DATE, FirstDayOfWeek() - day);
Time();
}
/
**
* 获得当⽉起始时间
*
* @return
*/
public static Date getStartMounth(Date date) {
Calendar calendar = Instance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Time();
}
/**
* 获取当前季度起始时间
*
* @return
*/
public static Date getStartQuarter(Date date) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论