Here you can find the source of convertDateToString(Date date)
Parameter | Description |
---|---|
date | Instance of java.util.Date. |
public static String convertDateToString(Date date)
//package com.java2s; /* The contents of this file are subject to the license and copyright terms * detailed in the license directory at the root of the source tree (also * available online at http://fedora-commons.org/license/). *//* www . j av a 2s. c o m*/ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { private static final Date ONE_CE = new Date(-62135769600000L); /** * Converts an instance of java.util.Date into a String using the date * format: yyyy-MM-ddTHH:mm:ss.SSSZ. * * @param date * Instance of java.util.Date. * @return ISO 8601 String representation (yyyy-MM-ddTHH:mm:ss.SSSZ) of the * Date argument or null if the Date argument is null. */ public static String convertDateToString(Date date) { return convertDateToString(date, true); } /** * Converts an instance of java.util.Date into an ISO 8601 String * representation. Uses the date format yyyy-MM-ddTHH:mm:ss.SSSZ or * yyyy-MM-ddTHH:mm:ssZ, depending on whether millisecond precision is * desired. * * @param date * Instance of java.util.Date. * @param millis * Whether or not the return value should include milliseconds. * @return ISO 8601 String representation of the Date argument or null if * the Date argument is null. */ public static String convertDateToString(Date date, boolean millis) { if (date == null) { return null; } else { DateFormat df; if (millis) { df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); } else { df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); } df.setTimeZone(TimeZone.getTimeZone("UTC")); if (date.before(ONE_CE)) { StringBuilder sb = new StringBuilder(df.format(date)); sb.insert(0, "-"); return sb.toString(); } else { return df.format(date); } } } }