Java Today getToday()

Here you can find the source of getToday()

Description

Get date string as format of "yyyy-MM-dd"

License

Open Source License

Declaration

public static String getToday() 

Method Source Code


//package com.java2s;

import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

public class Main {
    private static SimpleDateFormat defaultDateFormatter;

    /**//from  w w  w  . j  a va  2 s . c om
     * Get date string as format of "yyyy-MM-dd"
     * @return
     */
    public static String getToday() {
        return getDefaultDateFormatter().format(getCalendar().getTime());
    }

    /**
     * Get simple date formatter with default time zone of GMT+8
     * @return simple date formatter
     */
    public static SimpleDateFormat getDefaultDateFormatter() {
        if (defaultDateFormatter == null) {
            defaultDateFormatter = new SimpleDateFormat("yyyy-MM-dd");
            defaultDateFormatter.setTimeZone(getDefaultTimeZone());
        }
        return defaultDateFormatter;
    }

    /**
     * Get calendar instance with default time zone of GMT+8 
     * @return Calendar instance
     */
    public static Calendar getCalendar() {
        TimeZone.setDefault(getDefaultTimeZone());
        return Calendar.getInstance();
    }

    /**
     * Get default time zone of China
     * @return default time zone
     */
    public static TimeZone getDefaultTimeZone() {
        return TimeZone.getTimeZone("GMT+8");
    }
}

Related

  1. getToday()
  2. getToday()
  3. getToday()
  4. getToday()
  5. getToday()
  6. getToday()
  7. getToday()
  8. getToday()
  9. getToday()