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

Date newDate = DateUtils.addHours(testDate, 1);

DateUtils.addHours

Edit:

Here u are:

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 に答える