Java Date Format convertDateToString(Date date, boolean millis)

Here you can find the source of convertDateToString(Date date, boolean millis)

Description

convert Date To String

License

Apache License

Declaration

public static String convertDateToString(Date date, boolean millis) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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);

    public static String convertDateToString(Date date, boolean millis) {
        if (date == null) {
            return null;
        } else {/*from   w ww.j  a  va  2  s  . co  m*/
            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");
            }
            df.setTimeZone(TimeZone.getTimeZone("GMT"));

            if (date.before(ONE_CE)) {
                StringBuilder sb = new StringBuilder(df.format(date));
                sb.insert(0, "-");
                return sb.toString();
            } else {
                return df.format(date);
            }
        }
    }
}

Related

  1. convertDateToString(Date date)
  2. convertDateToString(Date date)
  3. convertDateToString(Date date)
  4. convertDateToString(Date date)
  5. convertDateToString(Date date)
  6. convertDateToString(Date date, int addHours)
  7. convertDateToString(Date date, String dateFormat)
  8. convertDateToString(Date date, String format)
  9. convertDateToString(Date date, String pattern)