开发者

java add hours to system date and in yyyyMMddHHmmss format

开发者 https://www.devze.com 2023-03-29 06:42 出处:网络
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 th

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

精彩评论

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