I need to convert a Java Date object to a string that is the same format as JavaScript Dates when they are converted to a string. On our server we have JavaScript dates that
To format the date exactly like that, we may need to format date and timezone separately, e,g,:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss");
Date date = new Date();
String dateStr = format.format(date);
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset();
String text = String.format("%s%02d%02d", offset >= 0 ? "+" : "-", offset / 3600000, (offset / 60000) % 60);
String timeZoneText = "GMT" + text + " (" + tz.getDisplayName(true, tz.SHORT, Locale.getDefault())+")";
System.out.println(dateStr + " " + timeZoneText);