写在前面
这个没什么多说的就是自己项目上用到的一些工具类,分享出来,供需要的小伙伴可以使用
当然不是很全,大家可根据自己的需要进行补充~~
代码
日期工具类:
package com.cn.zhangb.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
*/
public class DateUtil {
/**
* @param date
* @param format
* 将日期转化为String字符串
* @return
*/
public static String dateToString(Date date, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
if (date == null) {
return null;// 不要返回空字符串,mybatis底层中entity中使用此方法会报异常.
} else {
return simpleDateFormat.format(date);
}
}
/**
*
* @param date
* @param format
* @return 将String类型时间转化为日期
*/
public static Date stringToDate(String dateStr, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = new Date();
try {
date = simpleDateFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取系统当前时间并转化为String
* @return
*/
public static String getCurrentDateStr() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(new Date());
}
/**
* 时间戳转换成日期格式字符串 精确到秒
*
* @param seconds
* @param format
* @return
*/
public static String timeStamp2Date(String seconds, String format) {
if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
return "";
}
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds + "000")));
}
/**
* 日期格式字符串转换成时间戳
*
* @param date_str
* @param format
* @return
*/
public static String date2TimeStamp(String datestr, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return String.valueOf(sdf.parse(datestr).getTime() / 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
/**
* 取得当前时间戳 精确到秒
*/
public static String timeStamp() {
long time = System.currentTimeMillis();
String t = String.valueOf(time / 1000);
return t;
}
/**
* 将String类型转化成你所传入的时间类型
* @param date
* @param pattern
* @return
*/
public static String formatByPattern(Date date, String pattern) {
if (date == null) {
return "";
}
String result = "";
try {
SimpleDateFormat format = new SimpleDateFormat(pattern);
result = format.format(date);
} catch (Exception e) {
return "";
}
return result;
}
/**
* 时间戳转换成日期格式字符串 精确到毫秒
* @param seconds
* @param format
* @return
*/
public static String millisecond2Date(String seconds, String format) {
if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
return "";
}
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds)));
}
}
下面的是时间工具类:
package com.cn.zhangb.util;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具类
*
*/
public class TimeUtil {
/**
* 获取某一天的时间
*
* @param date
* @param count
* @return
*/
public static Date getNextDay(Date date, int count) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_MONTH, count);
return c.getTime();
}
/**
* 获取给定时间的所属月份
*
* @param date
* @return
*/
public static String getYYYYMM(Date date) {
if (date == null) {
return null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
return sdf.format(date);
}
}
/**
* 获取给定时间的所属月份
*
* @param date
* @return
*/
public static String getYYYYMMddHHmm(Date date) {
if (date == null) {
return null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return sdf.format(date);
}
}
/**
* 将日期字符串转化为日期
*
* @param dateString
* @param mask
* @return
* @throws Exception
*/
public static Timestamp convertStringToDate(String dateString, String mask) throws Exception {
if (dateString == null) {
return null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat(mask);
return new Timestamp(sdf.parse(dateString).getTime());
}
}
/**
* 根据给定月份获取其他月份
*
* @param month
* @param mask
* @param count
* @return
* @throws Exception
*/
public static String getOtherMonth(String month, String mask, int count) throws Exception {
if (month == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(mask);
Date date = sdf.parse(month);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, count);
return sdf.format(cal.getTime());
}
}