2

我有一个代码

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
4

3 回答 3

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
于 2013-09-06T06:59:18.853 回答
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);
 }
于 2013-09-06T06:48:02.647 回答
0

日期 newDate = DateUtils.addHours(testDate, 1);

DateUtils.addHours

编辑:

你在这里:

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);
于 2013-09-06T06:51:50.503 回答