开发者

java add hours to system date and in yyyyMMddHHmmss format

I am ge开发者_运维技巧tting input as some date/time in hours(integer) eg: 2 hrs I want to add this input hours to system date in yyyyMMddHHmmss format. How can i do this?


You could do something like this (assuming you meant format instead of parse):

int hoursToAdd = 2; //or from input
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, hoursToAdd);
System.out.println(new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime()));


Using the joda-time library:

System.out.println(DateTimeFormat.forPattern("yyyyMMddHHmmss")
                                 .print(new DateTime().plusHours(2)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜