Java Now now()

Here you can find the source of now()

Description

now

License

Open Source License

Declaration

public static final String now() 

Method Source Code


//package com.java2s;
import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
    private static Lock lock = new ReentrantLock();

    public static final String now() {
        lock.lock();/* w  w w  . j  a va 2s . c o  m*/
        try {
            return getDateFormat().format(new Date());
        } finally {
            lock.unlock();
        }
    }

    public static String format(Date date) {
        return getDateFormat().format(date);
    }

    /**
     * Returns a formatter for ISO 8601 format as described at http://www.cl.cam.ac.uk/~mgk25/iso-time.html
     * @return
     */
    public static final DateFormat getDateFormat() {
        lock.lock();
        try {
            TimeZone tz = TimeZone.getTimeZone("UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            df.setTimeZone(tz);
            return df;
        } finally {
            lock.unlock();
        }
    }
}

Related

  1. Now()
  2. now()
  3. now()
  4. now()
  5. now()
  6. now()
  7. now()
  8. now(String dateFormat)
  9. now(String dateFormat)