Java Time Now getNow()

Here you can find the source of getNow()

Description

Get date string as format of "yyyy-MM-dd HH:mm:ss.SSS"

License

Open Source License

Declaration

public static String getNow() 

Method Source Code


//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.TimeZone;

public class Main {
    /**//from  ww  w  .  java2 s.  co  m
     * Get date string as format of "yyyy-MM-dd HH:mm:ss.SSS"
     * @return
     */
    public static String getNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        sdf.setTimeZone(getDefaultTimeZone());
        return sdf.format(getCalendar().getTime());
    }

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

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

Related

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