我有一个代码
String date = 05/09/13 10.55 PM;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;
testDate = sdf.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..." + newFormat);
这个给我输出
05/09/13 10:55:00 PM
我真正需要的是什么:
05/09/13 11:55:00 PM //i want to add an hour to the date I got
答案 0 :(得分:2)
使用以下代码这将增加1小时并打印所需的结果。
String date = "05/09/13 10.55 PM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;
try {
testDate = sdf.parse(date);
// Add 1 hour logic
Calendar tmpCalendar = new GregorianCalendar();
tmpCalendar.setTime(testDate);
tmpCalendar.add(Calendar.HOUR_OF_DAY, 1);
testDate = tmpCalendar.getTime();
// Continue with your logic
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..." + newFormat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<强>输出强>
.....Date...05/09/2013 11:55:00 PM
答案 1 :(得分:0)
使用此代码,
cal.add(Calendar.HOUR, +1);
使用日历格式就是这样,
for(int i=0;i<=24;i++)
{
cs=cal.get(Calendar.HOUR_OF_DAY);
s= cal.get(Calendar.MINUTE);
t=cs+":00";
cal.add(Calendar.HOUR, +1);
timing.add(t);
}
答案 2 :(得分:0)
日期newDate = DateUtils.addHours(testDate,1);
修改强>
你在这里:
String date = 05/09/13 10.55 PM;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = sdf.parse(date);
Date newDate = DateUtils.addHours(testDate, 1);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(newDate);
System.out.println(".....Date..." + newFormat);