public class DateUtil {
public static final String FORMAT0 = "yyyy-MM-dd";
public static final String FORMAT1 = "yyyyMMdd";
public static final String FORMAT2 = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT3 = "yyyyMMddHHmmss";
private static String[] DAYWEEK = {"周一","周二","周三","周四","周五","周六","周日"};
/**
* 根据入口参数的年月日计算周几
* @param dateStr 日期字符串
* @param dateFmt 格式字符串
* @param weekPre 前缀
* @return 前缀 + (一二三四五六日)
*/
public static String getWeek(String dateStr,String dateFmt, String weekPre) {
Date dat = getDate(dateStr, dateFmt);
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(dat);
int w=cal.get(java.util.Calendar.DAY_OF_WEEK)-1;
String weekString = "";
String wPre = null == weekPre?"星期":weekPre;
switch (w) {
case 0:{
weekString = "日";
break;
}
case 1:{
weekString = "一";
break;
}
case 2:{
weekString = "二";
break;
}
case 3:{
weekString = "三";
break;
}
case 4:{
weekString = "四";
break;
}
case 5:{
weekString = "五";
break;
}
case 6:{
weekString = "六";
break;
}
}
return wPre + weekString;
}
/**
* 判断当前日期是星期几
*
* @param pTime 修要判断的时间
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
public static String dayForWeek(String fmt, String pTime) {
SimpleDateFormat format = new SimpleDateFormat(fmt);
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
e.printStackTrace();
}
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return DAYWEEK[dayForWeek-1];
}
/**
* 判断当前日期是星期几
*
* @param pTime 修要判断的时间
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
public static int dayForWeekNum(String fmt, String pTime) {
SimpleDateFormat format = new SimpleDateFormat(fmt);
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
e.printStackTrace();
}
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
/**
* 判断当前日期是星期几
*
* @param pTime 修要判断的时间
* @return dayForWeek 判断结果
*/
public static int dayNumberForWeek(String fmt, String pTime) {
SimpleDateFormat format = new SimpleDateFormat(fmt);
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
e.printStackTrace();
}
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
/**
* 当前星期几
* @return
*/
public static int getCurrDay() {
int retObj = -1;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
retObj = c.get(Calendar.DAY_OF_WEEK);
if ( retObj > 1 ) {
retObj = retObj - 1;
} else if ( retObj == 1 ) {
retObj = 7;
}
return retObj;
}
/**
* 当前小时
* @return
*/
public static int getCurrHour() {
int retObj = -1;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
retObj = c.get(Calendar.HOUR_OF_DAY);
return retObj;
}
/**
* 当前分钟
* @return
*/
public static int getCurrMinute() {
int retObj = -1;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
retObj = c.get(Calendar.MINUTE);
return retObj;
}
/***
* 返回星期几,星期日返回7
* @param fmt
* @param date
* @return
*/
public static int getDayByDateAndFmt(String fmt, String date) {
int retObj = -1;
Calendar c = Calendar.getInstance();
Date dat = null;
try {
dat = new SimpleDateFormat(fmt).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(dat);
retObj = c.get(Calendar.DAY_OF_WEEK);
if ( retObj > 1 ) {
retObj = retObj - 1;
} else if ( retObj == 1 ) {
retObj = 7;
}
return retObj;
}
/**
* 获得指定日期的前几天
*
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getDayBefore(String format, int n) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - n);
String dayBefore = new SimpleDateFormat(format).format(c.getTime());
return dayBefore;
}
/**
* 彩票提前截止时间设置
*
*/
public static Date getDayMiniteBefore(String fmt, String dateTime, int n) {
SimpleDateFormat format = new SimpleDateFormat(fmt);
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(dateTime));
} catch (ParseException e) {
e.printStackTrace();
}
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) - n);
//String dayBefore = new SimpleDateFormat(fmt).format(c.getTime());
return c.getTime();
}
/**
* 获得指定日期的后几天
*
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getDayNext(String format, int n) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + n);
String dayBefore = new SimpleDateFormat(format).format(c.getTime());
return dayBefore;
}
/**
* 获得当天的开始时间
*
* @param format
* @return
* @throws Exception
*/
public static String getTodayBeginTime(String format) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
String beginTime = new SimpleDateFormat(format).format(c.getTime());
return beginTime;
}
/**
* 获得当天的结束时间
* @param format
* @return
* @throws Exception
*/
public static String getTodayEndTime(String format) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
String endTime = new SimpleDateFormat(format).format(c.getTime());
return endTime;
}
/**
* 按指定格式取得当前系统日期
* @return 当前系统日期
* 如fmt="yyyy年MM月dd",返回2007年11月20日
* 如fmt="hh:mm:ss",返回13:40:20
* Y/y-年;M-月;D/d日期;H-小时(24小时);h-小时(12小时),m-分钟,S/s-秒
*/
public static String getNowDateByFmt(String fmt) {
Date now = new Date();
fmt = fmt.replace('Y','y');
fmt = fmt.replace('D','d');
//fmt = fmt.replace('S','s');
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
String nowDate = sdf.format(now);
return nowDate;
}
/**
* 把字符串转成日期对象
* @param strTime 时间字符串
* @param timeFmt 时间格式
* @return
*/
public static Date getDate(String strTime,String timeFmt) {
DateFormat fmt = new SimpleDateFormat(timeFmt);
Date date = null;
try {
date = fmt.parse(strTime);
} catch (ParseException e) {
}
return date;
}
/**
* 格式化日期
* @param timeFmt
* @param date
* @return
*/
public static String formatDay(String timeFmt, Date date) {
DateFormat fmt = new SimpleDateFormat(timeFmt);
return fmt.format(date);
}
public static Date formatDay2(String timeFmt, String date) {
DateFormat fmt = new SimpleDateFormat(timeFmt);
try {
return fmt.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 当前时间格式0
* @return "yyyy-MM-dd"
*/
public static String getDateFormat0() {
return dateFormate(FORMAT0, new Date());
}
/**
* 当前时间格式1
* @return "yyyyMMdd"
*/
public static String getDateFormat1() {
return dateFormate(FORMAT1, new Date());
}
/**
* 当前时间格式2
* @return "yyyy-MM-dd HH:mm:ss"
*/
public static String getDateFormat2() {
return dateFormate(FORMAT2, new Date());
}
/**
* 当前时间格式3
* @return "yyyyMMddHHmmss"
*/
public static String getDateFormat3() {
return dateFormate(FORMAT3, new Date());
}
/**
* 格式化当前时间字符串
* @param format
* @param date
* @return
*/
private static String dateFormate(String format, Date date) {
DateFormat fmt = new SimpleDateFormat(format);
return fmt.format(date);
}
/**
* 获得当前年
* @return
*/
public static int getNowYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
/**
* 获得当前月
* @return
*/
public static int getNowMonth() {
return Calendar.getInstance().get(Calendar.MONTH) + 1;
}
/**
* 获得当前天
* @return
*/
public static int getNowDay() {
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
/**
* 1970年至今时间差
* @return
*/
public static Integer getIntegerTimes(){
return Integer.valueOf(String.valueOf(Calendar.getInstance().getTimeInMillis()/1000));
}
/**
* 获取第一种时间格式
* @return "yyyy-MM-dd"
*/
public static String getDateFORMAT0Stirng(){
return DateUtil.FORMAT0;
}
/**获取第二种时间格式
* @return "yyyyMMdd"
*/
public static String getDateFORMAT1Stirng(){
return DateUtil.FORMAT1;
}
/**获取第三种时间格式
* @return "yyyy-MM-dd HH:mm:ss"
*/
public static String getDateFORMAT2Stirng(){
return DateUtil.FORMAT2;
}
/**获取第四种时间格式
* @return "yyyyMMddHHmmss"
*/
public static String getDateFORMAT3Stirng(){
return DateUtil.FORMAT3;
}
/***
* 指定时间的前n天
* @param specifiedDay
* @param beforeDAy
* @return
*/
public static String getSpecifiedDayBefore(String specifiedDay,int beforeDAy){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-beforeDAy);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
}
java日期设置类
最新推荐文章于 2023-12-25 08:15:00 发布