private final static String pattern = "yyyy-MM-dd HH:mm:ss";
public static String dateToString(){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date());
}
public static String dateToString(Date object){
if(null==object){
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(object);
}
public static Date stringToDate(String date) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(date);
}
public static Date stringToDate(String date,String pattern) throws ParseException{
if(date == null || "".equals(date.trim())||"null".equals(date.trim())){
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(date);
}
/**
* 获得当前时间精确到
* @return 20120531140759417
*/
public static String getStrNowDateHmsMs(){
Date tdate = new Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
int len = nowtime.length();
int between = 23-len;
for(int i=0;i<between;i++){
nowtime = nowtime+"0";
}
String nowYear = nowtime.substring(0, 4);
String nowMonth = nowtime.substring(5, 7);
String nowDay = nowtime.substring(8, 10);
String nowHour = nowtime.substring(11, 13);
String nowMinute = nowtime.substring(14, 16);
String nowSecond = nowtime.substring(17, 19);
String nowMilliSecond = nowtime.substring(20, 23);
String nowdate = nowYear + nowMonth + nowDay + nowHour + nowMinute + nowSecond + nowMilliSecond;
return nowdate;
}
/**
* 返回当前时间,格式:20120531
*/
public static String getStrNowDate() {
java.util.Date tdate = new java.util.Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
nowtime = nowtime.substring(0, 10);
String nowYear = nowtime.substring(0, 4);
String nowMonth = nowtime.substring(5, 7);
String nowDay = nowtime.substring(8, 10);
String nowdate = nowYear + nowMonth + nowDay;
return nowdate;
}
/**
* 返回当前月的第一天:20120501
*/
public static String getStrMonthFirstDay() {
java.util.Date tdate = new java.util.Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
nowtime = nowtime.substring(0, 10);
String nowYear = nowtime.substring(0, 4);
String nowMonth = nowtime.substring(5, 7);
String nowDay = "01";
String nowdate = nowYear + nowMonth + nowDay;
return nowdate;
}
/**
* 返回当前月的第一天:2012-05-01(格式与上面方法返回不同)
*/
public static String getMonthFirstDay() {
java.util.Date tdate = new java.util.Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
return nowtime.substring(0, 8)+"01";
}
/**
* 取得上个月字符串 : 201204
*/
public static String getStrPreviousMonth(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1 );
int month = cal.get(Calendar.MONTH)+1;
int year = cal.get(Calendar.YEAR);
String mon = String.valueOf(month);
if(mon.length()<2){
return year+"0"+mon;
}else{
return year+mon;
}
}
/**
* 取得上个月 2012-04
*/
public static String getPreviousMonth(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1 );
int month = cal.get(Calendar.MONTH)+1;
int year = cal.get(Calendar.YEAR);
String mon = String.valueOf(month);
if(mon.length()<2){
return year+"-0"+mon;
}else{
return year+"-"+mon;
}
}
/**
* 取得上个月最后一天 20120430
*/
public static String getStrPreviousMonthDate(){
Calendar cal=Calendar.getInstance();//当前日期
cal.set(Calendar.DATE,1);//设为当前月的1号
cal.add(Calendar.DATE,-1);//减一天,变为上月最后一天
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
return simpleDateFormat.format(cal.getTime());//输出
}
/**
* 取得上个月最后一天 2012-04-30
*/
public static String getPreviousMonthDate(){
Calendar cal=Calendar.getInstance();//当前日期
cal.set(Calendar.DATE,1);//设为当前月的1号
cal.add(Calendar.DATE,-1);//减一天,变为上月最后一天
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(cal.getTime());//输出20050430
}
/**
* 取得明天 2012-06-01
*/
public static String getTomorrowDate(){
Calendar cal=Calendar.getInstance();//当前日期
cal.add(Calendar.DATE,+1);//加一天
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(cal.getTime());//输出2005-05-01
}
/**
* 返回:20120531141511
*/
public static String getStrNowDateHms() {
java.util.Date tdate = new java.util.Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
nowtime = nowtime.substring(0, 19);
String nowYear = nowtime.substring(0, 4);
String nowMonth = nowtime.substring(5, 7);
String nowDay = nowtime.substring(8, 10);
String nowHour = nowtime.substring(11, 13);
String nowMinute = nowtime.substring(14, 16);
String nowSecond = nowtime.substring(17, 19);
String nowdate = nowYear + nowMonth + nowDay + nowHour + nowMinute + nowSecond;
return nowdate;
}
/**
* 2012-05-31
*/
public static String getNowDate() {
Date tdate = new Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
nowtime = nowtime.substring(0, 10);
return nowtime;
}
/**
* 2012-05-31 15:48:28
*/
public static String getNowDateHms() {
java.util.Date tdate = new java.util.Date();
String nowtime = new Timestamp(tdate.getTime()).toString();
nowtime = nowtime.substring(0, 19);
return nowtime;
}
/**
* 将日期转换成指定的格式
* @param date
* @param pattern
* @return
*/
public static String dateToString(Date date, String pattern) {
try{
DateFormat format = new SimpleDateFormat(pattern);
String str = format.format(date);
return str;
}catch(Exception e){
return null;
}
}
/**
* 根据传入的日期得到上个月yyyy-mm的形式,例如传入2009-11-11,得到2009-10
* @param date 传入的日期
* @return 转化后的日期
*/
public static String getPreviousMonth(String strDate){
java.sql.Date date = java.sql.Date.valueOf(strDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
DateFormat format = new SimpleDateFormat("yyyy-MM");
return format.format(calendar.getTime());
}
/**
* 根据日期获得上个月的月末
* @param strDate
* @return
*/
public static String getPreviousMonthEndDay(String strDate){
java.sql.Date date = java.sql.Date.valueOf(strDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
DateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(calendar.getTime());
}
/**
* 检查日期合法性
* @param s String
* @param dataFormat String 日期样式,比如"yyyy-mm-dd","yyyymmddHHmmSS"
* @return boolean
*/
public static boolean checkDate(String s,String dataFormat) {
boolean ret = true;
try {
DateFormat df = new SimpleDateFormat(dataFormat);
ret = df.format(df.parse(s)).equals(s);
}
catch (ParseException e) {
ret = false;
}
return ret;
}
public static Date string2Date(String s){
List<DateFormat> formats = new ArrayList<DateFormat>();
formats.add( new SimpleDateFormat("yyyy-MM-dd") );
formats.add( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") );
formats.add( new SimpleDateFormat("yy-MM-dd") );
formats.add( new SimpleDateFormat("yy-MM-dd HH:mm:ss") );
formats.add( new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss.SSS") );
formats.add( new SimpleDateFormat("MM/dd/yy hh:mm:ss a"));
formats.add( new SimpleDateFormat("MM/dd/yy") );
formats.add( new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") );
formats.add( new SimpleDateFormat("yyyy/MM/dd") );
formats.add( new SimpleDateFormat("yy/MM/dd") );
formats.add( new SimpleDateFormat("yy/MM/dd HH:mm:ss") );
formats.add( new SimpleDateFormat("EEE MMM d hh:mm:ss a z yyyy") );
formats.add( new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy") );
for(DateFormat format:formats){
try {
Date d = format.parse(s);
return d;
} catch (ParseException e) {
}
}
return null;
}
/**
* 获得当年1月1日的日期
*/
public static String getFirstDayOfYear(String str) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
String year = null;
try {
date = sd.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
year = sdf.format(calendar.getTime());
return year + "-01-01 00:00:00";
} catch (ParseException e) {
e.printStackTrace();
}
return year;
}
/**
* 判断时间是否在凌晨19:00:00 ~~23:59:59
*/
public static boolean is7To12(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String c = sdf.format(cal.getTime());
Integer time = Integer.parseInt(c.replace(":",""));
if(time>=190000 && time <=235959){
return true;
}
return false;
}
/**
* 判断时间是否在凌晨00:00:00 ~~05:00:00
*/
public static boolean is0To5(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String c = sdf.format(cal.getTime());
Integer time = Integer.parseInt(c.replace(":",""));
if(time>=1 && time <=50000){
return true;
}
return false;
}
/**
* 根据传入日期param参数获取前一天日期
* @param param: yyyy-MM-dd HH:mm:ss
*/
public static String getYesterDay(String param){
SimpleDateFormat frt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = frt.parse(param);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
return frt.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 根据传入日期param参数获取35天前的日期
* @param param: yyyy-MM-dd HH:mm:ss
**/
public static String getBefore35(String param){
SimpleDateFormat frt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = frt.parse(param);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -34);
return frt.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 判断当前日期是否是一周的第一天
*/
public static boolean isFirstDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 这里设置从周一开始,若需要根据系统时区自动获取,则采用下边的方式
Calendar cal2 = Calendar.getInstance();
return cal.equals(cal2);
}
/**
* 判断当前日期是否是一月的第一天
*/
public static boolean isFirstDayOfMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);// 这里设置从周一开始,若需要根据系统时区自动获取,则采用下边的方式
Calendar cal2 = Calendar.getInstance();
return cal.equals(cal2);
}
/**
* 当前日期的获得本月1号日期 calendar 当前日期
*/
public static String getFirstDateOfCurMonth(Calendar calendar) {
Calendar first = Calendar.getInstance();
first.setTime(calendar.getTime());
int minDay = first.getMinimum(Calendar.DAY_OF_MONTH);
minDay = first.getMinimum(Calendar.DAY_OF_MONTH);
first.set(Calendar.DAY_OF_MONTH, minDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(first.getTime());
}
/**
* 获得上个月1号
*/
public static Date getFirstDateOfLastMonth(Calendar calendar) {
Calendar first = Calendar.getInstance();
first.setTime(calendar.getTime());
int minDay = first.getMinimum(Calendar.DAY_OF_MONTH);
first.add(Calendar.MONTH, -1);
minDay = first.getMinimum(Calendar.DAY_OF_MONTH);
first.set(Calendar.DAY_OF_MONTH, minDay);
return first.getTime();
}
/**
* 获取上个月最后一天的日期
*/
public static Date getLastDateOfLastMonth(Calendar calendar) {
Calendar last = Calendar.getInstance();
last.setTime(calendar.getTime());
int maxDay = last.getMaximum(Calendar.DAY_OF_MONTH);
last.add(Calendar.MONTH, -1);
maxDay = last.getActualMaximum(Calendar.DAY_OF_MONTH);
last.set(Calendar.DAY_OF_MONTH, maxDay);
return last.getTime();
}
/**
* 获得上周第一天的日期
*/
public static Date getFirstDateOfLastWeek(Calendar calendar) {
Calendar first = Calendar.getInstance();
first.setTime(calendar.getTime());
first.setFirstDayOfWeek(Calendar.MONDAY);
int index = first.getFirstDayOfWeek();
first.set(Calendar.DAY_OF_WEEK, index);
first.add(Calendar.DAY_OF_WEEK, -7);
return first.getTime();
}
/**
* 获得上周最后一天的日期
*/
public static Date getLastDateOfLastWeek(Calendar calendar) {
Calendar last = Calendar.getInstance();
last.setTime(calendar.getTime());
last.setFirstDayOfWeek(Calendar.MONDAY);
int index = last.getFirstDayOfWeek();
last.set(Calendar.DAY_OF_WEEK, index);
last.add(Calendar.DAY_OF_WEEK, -1);
return last.getTime();
}
/**
* 获得日期date所在的当前周数
*/
@SuppressWarnings("static-access")
public static String getConvertoWeekDate(String date, String inputFormat)
throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myd = format.parse(date);
SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);
Calendar ccc = Calendar.getInstance();
ccc.setTime(myd);
ccc.setFirstDayOfWeek(Calendar.MONDAY);
/**/
String str = date.substring(0, 4);
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
Date compara = tempFormat.parse(str + "-01-01");
Calendar CalP = Calendar.getInstance();
CalP.setTime(compara);
int x = CalP.get(CalP.DAY_OF_WEEK);
/**/
if (x != 2) {
sdf.setCalendar(ccc);
String s = sdf.format(ccc.getTime());
String s1 = s.substring(0, 4);
String s2 = s.substring(4, 6);
Integer i = Integer.parseInt(s2);
if (i > 1) {
i = i - 1;
}
s2 = i + "";
if ((i + "").length() < 2) {
s2 = "0" + i;
}
return s1 + s2;
} else {
sdf.setCalendar(ccc);
return sdf.format(ccc.getTime());
}
}
/**
* 获得前一天开始的日期
*/
public static String getYesterdayStart(Calendar calendar) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getTime());
cal.add(Calendar.DATE, -1);
return sdf.format(cal.getTime()) + " 00:00:00";
}
/**
* 获得前一天结束的日期
*/
public static String getYesterdayEnd(Calendar calendar) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getTime());
cal.add(Calendar.DATE, -1);
return sdf.format(cal.getTime()) + " 23:59:59";
}
/**
* 获得前一天的日期
*/
public static String getYesterday(Calendar calendar) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getTime());
cal.add(Calendar.DATE, -1);
return sdf.format(cal.getTime());
}
日期工具类 DateUtil
最新推荐文章于 2024-08-26 14:35:51 发布