【问题标题】:Add 1 hour to current time in android [duplicate]在android中将1小时添加到当前时间[重复]
【发布时间】:2012-12-01 00:29:01
【问题描述】:
可能重复:
How to increment time by 1 hour
我正在使用此代码获取当前日期和时间
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 小时格式的答案。
【问题讨论】:
标签:
java
android
calendar
【解决方案1】:
尝试:
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());
【解决方案2】:
Calendar now = Calendar.getInstance();
now.add(Calendar.HOUR,1);
System.out.println(now.getTime());
【解决方案3】:
您可以在将日期传递给 sdf.format(date) 之前像这样增加日期
Date a=new Date();
a.setTime(System.currentTimeMillis()+(60*60*1000));