See the documentation for SimpleDateFormat and Calendar for more clues.
- String myDateString = "6/21/2012 3:12:00 AM";
- DateFormat myDateFormat = new SimpleDateFormat("M/d/yyyy h:mm:ss a");
- /* Conversion */
- Date myOldTime = null;
- try {
- myOldTime = myDateFormat.parse( myDateString );
- } catch (Exception e) {
- println("Could not parse date.");
- }
- /* Calculation */
- if( myOldTime != null ) { // Did we succeed parsing the date string?
- Calendar myCalendar = GregorianCalendar.getInstance(); // We need a calendar to calculate stuff
- myCalendar.setTime( myOldTime ); // Put the time in the calendar
- myCalendar.add( Calendar.DATE, 14 ); // Add 14 days
- myCalendar.add( Calendar.HOUR, 8 ); // Add 8 hours
- Date myNewTime = myCalendar.getTime(); // Get the new time back
- /* Format and output */
- println( "Time at the beginning was:" );
- println( myDateFormat.format( myOldTime ) );
- println( "Time is now:" );
- println( myDateFormat.format( myNewTime ) );
- }
Brilliant!
Exactly the help I needed. I knew there had to be an easier way than the miles of character reading code that I wrote. I made one tweak to the code for the re-formatting of the date.
String myDateString = "6/21/2012 3:12:00 AM";
DateFormat myDateFormat = new SimpleDateFormat("M/d/yyyy h:mm:ss a");
DateFormat myDateFormat2 = new SimpleDateFormat("MMM dd hh:00");
/* Conversion */
Date myOldTime = null;
try {
myOldTime = myDateFormat.parse( myDateString );
}
catch (Exception e) {
println("Could not parse date.");
}/* Calculation */
if( myOldTime != null ) { // Did we succeed parsing the date string?
Calendar myCalendar = GregorianCalendar.getInstance(); // We need a calendar to calculate stuff
myCalendar.setTime( myOldTime ); // Put the time in the calendar
myCalendar.add( Calendar.DATE, 14 ); // Add 14 days
myCalendar.add( Calendar.HOUR, 8 ); // Add 8 hours
Date myNewTime = myCalendar.getTime(); // Get the new time back