Hi
I have a code in java to generate a sitemap xml, and to get the last modified date using the following line of code:
oPrint.println(" <lastmod>"+ doc.getLastModified().toString()+"</lastmod>");

The output in the browser when calling the xml file is <lastmod>23/05/2014 16:48:24 ZE4</lastmod> which is in "dd/mm/yyyy"format.

However, from this link, it states that the format has to be in YYYY-MM-DD format.

How can I convert my current output to the Google one?

Dani AI

Generated

Short summary and a practical fix.

The string you saw from doc.getLastModified().toString() (example: 05/05/2014 08:43:08 ZE4) is a human-readable date produced by Date.toString() and can include nonstandard timezone labels (like ZE4) that are not suitable for sitemaps. Google accepts either a date-only YYYY-MM-DD or a full W3C datetime with timezone (for example 2014-05-23T16:48:24+02:00). If the Date object is available, format that Date directly — do not rely on parsing the toString() output.

Recommended approaches:

  • Java 8+ (preferred — uses java.time and produces the correct ISO 8601 offset automatically):

    // Java 8+
    Instant instant = doc.getLastModified().toInstant();
    ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
    String lastmod = zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    oPrint.println("  <lastmod>" + lastmod + "</lastmod>");
  • Java 7 / older (SimpleDateFormat fallback; insert colon into RFC822 offset to produce ISO 8601):

    // Java 7
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    String lastmod = sdf.format(doc.getLastModified());       // e.g. 2014-05-23T16:48:24+0200
    lastmod = lastmod.substring(0, lastmod.length()-2) + ":" + lastmod.substring(lastmod.length()-2);
    oPrint.println("  <lastmod>" + lastmod + "</lastmod>");   // e.g. 2014-05-23T16:48:24+02:00

Troubleshooting notes:

  • As suggested, formatting APIs are the right tool; as asked, the trouble is usually timezone parsing. Zone abbreviations like ZE4 are nonstandard and often cannot be parsed — prefer numeric offsets or use the Date/Instant directly.
  • If you only need the date (no time), emit yyyy-MM-dd and avoid timezone headaches.
  • If the source only gives weird labels, either map them to offsets explicitly or ask the source to provide a numeric offset or epoch millis.

(Background: later noted they resolved the ZE4 issue; the examples above will produce sitemap-friendly timestamps for others who hit the same problem.)

Recommended Answers

All 4 Replies

Addon:
Based on the above code, 05/05/2014 08:43:08 ZE4, how can I get it in the Google format, that is YYYY-MM-DDThh:mm:ssTZD

Did you look at the links stultuske posted? What specifically about SimpleDateFormat are you having trouble with?

Thnks for the links stultuske, I was actually having problems to resolve the TimeZone from my initial string to convert to the new one, since I did not know what the ZE4 was standing for (since it was generated automatically), but found the solution in the end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.