共计 5954 个字符,预计需要花费 15 分钟才能阅读完成。
罕用操作日期和操作工夫戳办法,拿着间接用系列
package com.gxw.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTime {
// 日期工夫 24 小时格局字符串
public final static String formatDateTime24H = "yyyy-MM-dd HH:mm:ss";
// 日期工夫 12 小时格局字符串
public final static String formatDateTime12H = "yyyy-MM-dd hh:mm:ss";
// 日期格局字符串
public final static String formatDate = "yyyy-MM-dd";
// 工夫 24 格局字符串
public final static String formatTime24H = "HH:mm:ss";
// 工夫 12 格局字符串
public final static String formatTime12H = "hh:mm:ss";
/**
* 获取以后日期戳 yyyy-MM-dd hh:mm:ss
* @return
*/
public static String getNowDateStamp(){return String.valueOf(new Date().getTime());
}
/**
* 获取以后日期字符串 yyyy-MM-dd hh:mm:ss
* @return
*/
public static String getNowDateStr(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return sdf.format(date);
}
/**
* 工夫戳转日期
* @param stamp
* @param format
* @return
*/
public static String getStampCaseDate(String stamp, String format){SimpleDateFormat sdf = new SimpleDateFormat(format);
long stp = new Long(stamp);
Date date = new Date(stp);
String str = sdf.format(date);
return str;
}
/**
* 工夫戳转日期
* @param stamp
* @return
*/
public static String getStampCaseDateFor24(String stamp){
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
long stp = new Long(stamp);
Date date = new Date(stp);
String str = sdf.format(date);
return str;
}
/**
* 字符串日期转换工夫戳
* @param date
* @return
*/
public static String getDateCaseStamp(String date){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {Date d = sdf.parse(date);
return String.valueOf(d.getTime());
} catch (ParseException e) {e.printStackTrace();
}
return "";
}
/**
* 字符串日期转换工夫戳
* @param date
* @return
*/
public static String getDateStrCaseStamp(String date, String format){SimpleDateFormat sdf = new SimpleDateFormat(format);
try {Date d = sdf.parse(date);
return String.valueOf(d.getTime());
} catch (ParseException e) {e.printStackTrace();
}
return "";
}
/**
* 获取当周的周一日期
* @return
*/
public static String getNowWeekStartStamp(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 取得以后日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if(dayWeek==1){dayWeek = 8;}
// 依据日历的规定,给以后日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
Date mondayDate = cal.getTime();
mondayDate.setHours(00);
mondayDate.setMinutes(00);
mondayDate.setSeconds(00);
String weekBegin = sdf.format(mondayDate);
// System.out.println("所在周星期一的日期:" + weekBegin);
return String.valueOf(mondayDate.getTime());
}
/**
* 获取当周的周天日期
* @return
*/
public static String getNowWeekEndStamp(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 取得以后日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if(dayWeek==1){dayWeek = 8;}
// 依据日历的规定,给以后日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
Date sundayDate = cal.getTime();
sundayDate.setHours(23);
sundayDate.setMinutes(59);
sundayDate.setSeconds(59);
String weekEnd = sdf.format(sundayDate);
// System.out.println("所在周星期日的日期:" + weekEnd);
return String.valueOf(sundayDate.getTime()).substring(0,10) + "999";
}
/**
* 将当天工夫戳转换成当天最大工夫的工夫戳
* @param stamp
* @return
*/
public static String getStampMaxDate(String stamp){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(Long.parseLong(stamp));
String dateStr = sdf.format(date) + "23:59:59";
String rsStamp = getDateCaseStamp(dateStr);
return rsStamp;
}
/**
* 获取以后工夫的两月前工夫戳
* @return
*/
public static String getStamp2MonthBeforeDate(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, 1);// 设置以后月的一号
calendar.add(Calendar.MONTH, -2); // 0 示意以后月,- 2 就是以后月 -2
calendar.set(Calendar.HOUR_OF_DAY, 0);// 设置当天几时
calendar.set(Calendar.MINUTE, 0);// 设置当天几分
calendar.set(Calendar.SECOND, 0);// 设置当天几秒
calendar.set(Calendar.MILLISECOND, 0);// 设置当天几毫秒
long t = calendar.getTimeInMillis();
String s = sdf.format(new Date(t));
System.out.println(t);
System.out.println(s);
return String.valueOf(calendar.getTimeInMillis());
}
/**
* 校验日期是否非法
* @return
*/
public static boolean isValidDate(String s) {SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {fmt.parse(s);
return true;
} catch (Exception e) {
// 如果 throw java.text.ParseException 或者 NullPointerException,就阐明格局不对
return false;
}
}
/**
* <li> 性能形容:工夫相减失去天数
* @param beginDateStr
* @param endDateStr
* @return
* long
* @author Administrator
*/
public static long getDaySub(String beginDateStr,String endDateStr){
long day=0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate = null;
Date endDate = null;
try {beginDate = format.parse(beginDateStr);
endDate= format.parse(endDateStr);
} catch (ParseException e) {e.printStackTrace();
}
day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
//System.out.println("相隔的天数 ="+day);
return day;
}
/**
* 失去 n 天之后的日期
* @param days
* @return
*/
public static String getAfterDayDate(String days) {int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance(); // java.util 包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdfd.format(date);
return dateStr;
}
/**
* 失去 n 天之后是周几
* @param days
* @return
*/
public static String getAfterDayWeek(String days) {int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance(); // java.util 包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
return dateStr;
}
/**
* 失去 n 小时之后的工夫戳
* @param afterHours // 后几个小时
*/
public static String getAfterHours(int afterHours){Calendar canlendar = Calendar.getInstance(); // java.util 包
canlendar.add(Calendar.HOUR, afterHours);
Date date = canlendar.getTime();
return String.valueOf(date.getTime());
}
/**
* 失去 n 小时之前的工夫戳
* @param beforeHours // 前几个小时
*/
public static String getBeforeHours(int beforeHours){Calendar canlendar = Calendar.getInstance(); // java.util 包
canlendar.add(Calendar.HOUR, -beforeHours);
Date date = canlendar.getTime();
return String.valueOf(date.getTime());
}
}
正文完