我有一个密码
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
最佳答案
使用以下代码,这将增加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