码迷,mamicode.com
首页 > 其他好文 > 详细

DateUtils

时间:2018-01-31 14:46:23      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:day   case   计算   util   tin   pos   星期六   日期   获取   

public abstract class DateUtils {

public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String chDATE_FORMAT = "yyyy年MM月dd日";
public static final String defalut_date_format = "yyyy-MM-dd HH:mm:ss";
public static final String shortsdf_format = "HH:mm";
public static final String DATE_FORMAT_YEAR_MONTH = "MM-dd";
public static final String DATE_BASE_STRING = "yyyyMMddHHmmss";

/**
* 格式化日期为指定格式
* @param stringDate
* @param pattern
* @return
*/
public static Date formateStringToDate(String stringDate, String pattern) {
if(stringDate == null) {
return null;
}
try{
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date date = dateFormat.parse(stringDate);
return date;
}catch (Exception e){

}
return null;
}

public static String formatDateToString(Date date, String pattern){
if(date == null){
return null;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String dateFormatStr = simpleDateFormat.format(date);
return dateFormatStr;

}

public static String getWeekString(Date date){
if(date == null) {
return null;
}
String result = new SimpleDateFormat("u").format(date);
int r = Integer.parseInt(result);
String week = "";
switch (r) {
case 1:
week = "星期一";
break;
case 2:
week = "星期二";
break;
case 3:
week = "星期三";
break;
case 4:
week = "星期四";
break;
case 5:
week = "星期五";
break;
case 6:
week = "星期六";
break;
case 7:
week = "星期日";
break;
}

return week;
}

public static Date addHour(Date myDate, int amount) {
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.HOUR, amount);
return cal.getTime();
}

public static Date addDay(Date myDate, int amount) {
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DATE, amount);
return cal.getTime();
}

/**
* 取固当天内的定时间
* @param hour 时
* @param minute 分
* @param second 秒
* @return
*/
public static Date getTime(int hour, int minute, int second) {
return getTime(new Date(), hour, minute, second);
}

/**
* 取固定时间
* @param date
* @param hour
* @param minute
* @param second
* @return
*/
public static Date getTime(Date date, int hour, int minute, int second) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
cal.set(year, month, day, hour, minute, second);
return cal.getTime();
}


/**
* 计算两个日期间的相差分钟数
* @param date 大的日期
* @param beforeDate 小的日期
* @return
*/
public static int minutessBetween(Date date,Date beforeDate) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");
Calendar cal = Calendar.getInstance();
long time1 = 0;
long time2 = 0;
try{
cal.setTime(date);
time1 = cal.getTimeInMillis();
cal.setTime(beforeDate);
time2 = cal.getTimeInMillis();
}catch(Exception e){
return 0;
}
long between_minutes=(time1-time2)/(1000*60);
return Integer.parseInt(String.valueOf(between_minutes));
}

/**
* 普通经纪人显示房源的时间
* @return
*/
public static Date getGrabListDelayDate() {
Date beginDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginDate);
//calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - Integer.parseInt(Global.DEF_SUNXINGZHE_EARLY_TIME));
return new Date(calendar.getTime().getTime());

}

/**
* @Title: getFirstTimeOfDay
* @Description: 获取一天的开始时间
* @param date
* @return
* Date
* @throws
* @author zc
* @date 2015-12-10 下午3:05:44
*/
public static Date getFirstTimeOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* 取一天最后的时间
* @param date
* @return
*/
public static Date getLastTimeOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**今天向前推几天
* @param i
* @return
*/
public static Date getBeforeDays(int i){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -i);
return cal.getTime();
}

/**
* 今天与beforeDate相差几天
* @param beforeDate
* @return
*/
public static int todayBetween(Date beforeDate){
Date date = new Date();
return daysBetween(date,beforeDate);
}

/**
* 相差几天
*
* @param date
* @param beforeDate
* @return
*/
public static int daysBetween(Date date, Date beforeDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
long time1 = 0;
long time2 = 0;
try {
cal.setTime(date);
time1 = cal.getTimeInMillis();
cal.setTime(beforeDate);
time2 = cal.getTimeInMillis();
} catch (Exception e) {
return 0;
}
long between_minutes = (time1 - time2) / (1000 * 60 * 60 * 24);
return Integer.parseInt(String.valueOf(between_minutes));
}

DateUtils

标签:day   case   计算   util   tin   pos   星期六   日期   获取   

原文地址:https://www.cnblogs.com/ShenAiYiRen/p/8391356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!