获取指定时间格式、设置时间、比较日期格式

上一篇 / 下一篇  2019-04-18 20:16:00 / 个人分类:java基础


不多说,直接看代码

public classGetTimeUtil {

/**
*@Description:获取指定格式的时间,时间可设置
*@Parma:Date date:指定要修改的时间,如果不传默认就获取当前时间
* String format:指定时间的格式
* int day:指定的时间前移天数, 正数往后推(未来的时间),负数往前推(过去的时间)
* int hour:指定时间前移小时数
* int minute:指定时间前移分钟数
*@return:
*/
public staticStringgetFormatTime(Date date,String format, intday, inthour, intminute){
if("".equals(date) ||null== date){//如果不传date,就获取当前时间
date =newDate();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE,day);//修改时间
calendar.set(Calendar.HOUR,hour);
calendar.set(Calendar.MINUTE,minute);
date = calendar.getTime();
SimpleDateFormat df =newSimpleDateFormat(format);//指定格式
String time = df.format(date);

returntime;
}


/**
*@Description:判断指定日期是否符合指定格式
*@Parma:
* String dateStr:日期
* String format:格式
*@return:
*/
public staticBooleanisValidDate(String dateStr,String format){
booleanconvertSuccess =true;
SimpleDateFormat dateFormat =newSimpleDateFormat(format);
try{
dateFormat.setLenient(false);
dateFormat.parse(dateStr);
}catch(ParseException e){
convertSuccess=false;
}
returnconvertSuccess;
}
}

TAG: 时间格式 设置时间 时间前移 时间后移 比较日期格式

 

评分:0

我来说两句

我的栏目

日历

« 2024-03-17  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 992
  • 日志数: 2
  • 建立时间: 2015-01-05
  • 更新时间: 2019-04-18

RSS订阅

Open Toolbar