Java

Handling dates in Java is a bit of a nightmare. I am starting to create a page to hold the various nuggets of information I have found useful with a view to tidying this up later.

 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

final SimpleDateFormat sdf = new SimpleDateFormat("dd MMM HH:mm");

return cob.toYYYYMMDDString() + " @ "
+ sdf.format(new Date(this.fsItem.getPromotedDateTime(cob)));

final SimpleDateFormat sdf = new SimpleDateFormat("dd MMM HH:mm");
final Date date = new Date();
save(new Status(system, date2.toInt(), sdf.format(date.getTime()),

final Calendar c = new GregorianCalendar();
c.setTime(new Date(this.lastModified));

 

for (int i = 1; i < days; i++) {
newDate.add(Calendar.DAY_OF_YEAR, 1);

// Skip Weekends
if (newDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
newDate.add(Calendar.DAY_OF_YEAR, 2);
}
}

 

/**
* Convert XML date string into a calendar for the Header class
*
* @param dateString
* @return
*/
public static Calendar convertDateString(String dateString) {

Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
cal.setTime(format.parse(dateString));
} catch (ParseException e) {
e.printStackTrace();
}
return cal;
}