Java Date Format ISO getISODate()

Here you can find the source of getISODate()

Description

Get todays date and format into an ISO date string and return

License

Open Source License

Return

dateStr Todays date string in ISO format

Declaration

public static String getISODate() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

import java.util.TimeZone;

public class Main {
    private static SimpleDateFormat stdFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private static SimpleDateFormat isoFormatter = new SimpleDateFormat("yyyy-MM-dd");

    /**/*from  w  w w . j a v  a 2  s  .c  o m*/
     * Get todays date and format into an ISO date string and return
     * 
     * @return dateStr Todays date string in ISO format
     */
    public static String getISODate() {
        return isoFormatter.format(getToday());
    }

    /**
     * Format the given Date dt into an ISO date string and return
     * 
     * @param dt
     *            The given date
     * 
     * @return dateStr Todays date string in ISO format
     */
    public static String getISODate(Date dt) {
        return isoFormatter.format(dt);
    }

    /**
     * Format a Date into a string with the format of "yyyyMMddHHmmssSSS".
     * 
     * @param val
     *            The Date to format
     * 
     * @return dstr The formatted date string
     */
    public static String format(Date val) {
        if (val == null)
            return null;
        return stdFormatter.format(val);
    }

    /**
     * Format a Date into a string with the format of the format argument.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The Date to format
     * 
     * @return dstr The formatted date string
     */
    public static String format(String format, Date val) {
        if (val == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        return formatter.format(val);
    }

    public static String format(String format, Date val, Locale locale) {
        if (val == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format, locale);
        formatter.setLenient(false);
        return formatter.format(val);
    }

    /**
     * Format a Date into a string with the format of the format argument for
     * the given timezone.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The Date to format
     * @param timezone
     *            The timezone
     * 
     * @return dstr The formatted date string
     */
    public static String format(String format, Date val, String timezone) {
        if (val == null)
            return null;
        TimeZone tz = null;
        if (timezone == null || timezone.length() == 0) {
            tz = TimeZone.getDefault();
        } else {
            tz = TimeZone.getTimeZone(timezone);
        }
        return format(format, val, tz);
    }

    /**
     * This private format method is called by the public format method that
     * accepts a timezone.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The Date to format
     * @param timezone
     *            The timezone
     * 
     * @return dstr The formatted date string
     */
    private static String format(String format, Date val, TimeZone timezone) {
        if (val == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setTimeZone(timezone);
        formatter.setLenient(false);
        return formatter.format(val);
    }

    /**
     * Get todays date
     * 
     * @return date Todays Date
     */
    public static Date getToday() {
        return new Date();
    }
}

Related

  1. getISO8601String(Date date)
  2. getISO8601String(Date date)
  3. getISO8601Time()
  4. getISO8601Time(Date date)
  5. getISO8601UTCString(Date d)
  6. getISODate()
  7. getISODate(int i)
  8. getIsoDateFormat()
  9. getIsoDateFormat()