web-dev-qa-db-ja.com

androidで現在時刻に1時間を追加

可能性のある複製:
時間を1時間増やす方法

このコードを使用して現在の日付と時刻を取得しています

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
String currentDateandTime = sdf.format(new Date());
System.out.println("current time date" + currentDateandTime);

これに1時間追加したいと思います。検索しましたが、思い通りの答えが得られません。今私はこのようになっています:

current time date2012:12:13:12:05

2012:12:13:13:05のような24時間形式の回答が必要です。

13
Brinda K

次のように試してください:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
String currentDateandTime = sdf.format(new Date());

Date date = sdf.parse(currentDateandTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 1);

System.out.println("Time here "+calendar.getTime());
52

Sdf.format(date)に渡す前に、このような日付をインクリメントできます

   Date a=new Date();
   a.setTime(System.currentTimeMillis()+(60*60*1000));
19
Rahul Verma
Calendar now = Calendar.getInstance();
now.add(Calendar.HOUR,1);
System.out.println(now.getTime());
10
APriya