如何将"140654"字符串; 转化为小时并减去3分钟!
------解决方案--------------------
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
Date date = sdf.parse("140220");
转换成Date后可以用Calendar函数去转换
------解决方案--------------------
public static void main(String[] arg) throws ParseException {
String srcStr = "140654";
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
Date date = sdf.parse(srcStr);
date.setMinutes(date.getMinutes() - 3);
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf2.format(date));
}
------解决方案--------------------
我的思路是:
(1)把"140654" 转化为日期格式;
(2)参考如下函数:
public static java.util.Date getDateBeforeMinute(Date d, int minutes)
{
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.MINUTE) - minutes);
return now.getTime();
}
------解决方案--------------------
public static void main(String[] arg) {
String srcStr = "140654";
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
Calendar calendar = new GregorianCalendar();
try {
calendar.setTime(sdf.parse(srcStr));
calendar.add(calendar.MINUTE, -3);//减3分钟
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
------解决方案--------------------