关于时分字符串转成当前时间时分

2019-06-26  本文已影响0人  么么哒_86ed

因业务有对比时间判断,根据当前时分是否执行业务,数据库存的字段是类似于“8:00"的字符串,刚开始没有在意,直接转成date类型 SimpleDateFormat simdate=new SimpleDateFormat("HH:mm");,转完之后年份为1970年,时间校验怎么都是错的,应该要这样转变,才能取到当前的时间的时分

/**

* 转换时分

* @param parmas 时分参数 8:00

* @return

*/

public static Date changeDate(String parmas){

Assert.notNull(parmas,"参数不能为空");

String trim = parmas.trim();

Calendar cal=Calendar.getInstance();

Date now=new Date();

cal.setTime(now);

int year =cal.get(Calendar.YEAR);

int day = cal.get(Calendar.DATE);

int month = cal.get(Calendar.MONTH) +1;

SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd HH:mm");

try {

Date changeTime=simdate.parse(year+"-"+month+"-"+day+" "+trim);

return changeTime;

}catch (ParseException e) {

e.printStackTrace();

}

return null;

}

小功能!让我装下逼

上一篇下一篇

猜你喜欢

热点阅读