Here you can find the source of formatISO8601Date(Date d)
Parameter | Description |
---|---|
d | the input Date |
public static synchronized String formatISO8601Date(Date d)
//package com.java2s; /**/*from w ww . j a va2s .c o m*/ * The contents of this file are subject to the license and copyright * detailed in the LICENSE and NOTICE files at the root of the source * tree and available online at * * http://www.dspace.org/license/ */ import java.util.Date; import java.util.Calendar; import java.util.GregorianCalendar; import java.text.SimpleDateFormat; public class Main { private static SimpleDateFormat outFmtSecond = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"); private static SimpleDateFormat outFmtMillisec = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ"); private static Calendar outCal = GregorianCalendar.getInstance(); /** * Convert a Date to String in the ISO 8601 standard format. * The RFC822 timezone is almost right, still need to insert ":". * This method is synchronized because it depends on a non-reentrant * static DateFormat (more efficient than creating a new one each call). * * @param d the input Date * @return String containing formatted date. */ public static synchronized String formatISO8601Date(Date d) { String result; outCal.setTime(d); if (outCal.get(Calendar.MILLISECOND) == 0) { result = outFmtSecond.format(d); } else { result = outFmtMillisec.format(d); } int rl = result.length(); return result.substring(0, rl - 2) + ":" + result.substring(rl - 2); } }