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)));
精彩评论