Java Unix Date unixTime()

Here you can find the source of unixTime()

Description

unix Time

License

Apache License

Declaration

public static long unixTime() 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

    public static long unixTime() {
        java.util.Calendar cal1 = new GregorianCalendar(1970, 0, 1, 0, 0, 0);
        java.util.Calendar cal = java.util.Calendar.getInstance(java.util.Locale.CHINA);
        int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
        int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
        cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        long now = cal1.getTimeInMillis() / 1000;
        long now1 = cal.getTimeInMillis() / 1000;
        return now1 - now;
    }//from  w  w w . ja  v  a2 s . c o m

    public static int get(int field, Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(field);
    }

    /**
     * Adds to a date returning a new object.
     * The original date object is unchanged.
     *
     * @param date  the date, not null
     * @param calendarField  the calendar field to add to
     * @param amount  the amount to add, may be negative
     * @return the new date object with the amount added
     * @throws IllegalArgumentException if the date is null
     * @deprecated Will become privately scoped in 3.0
     */
    @Deprecated
    public static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }

    public static Date add(int field, Date date, int value) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int fieldNewValue = (c.get(field) + value);
        c.set(field, fieldNewValue);
        return c.getTime();
    }

    public static String getTime(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }

    /**
     * Sets the specified field to a date returning a new object.  
     * This does not use a lenient calendar.
     * The original date object is unchanged.
     *
     * @param date  the date, not null
     * @param calendarField  the calendar field to set the amount to
     * @param amount the amount to set
     * @return a new Date object set with the specified value
     * @throws IllegalArgumentException if the date is null
     * @since 2.4
     */
    private static Date set(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        // getInstance() returns a new object, so this method is thread safe.
        Calendar c = Calendar.getInstance();
        c.setLenient(false);
        c.setTime(date);
        c.set(calendarField, amount);
        return c.getTime();
    }
}

Related

  1. getUnixTime2NoneTime(String longTime)
  2. parseUnixDate(String paramName, String dateStr)
  3. stringUnixTime(String start, String end)
  4. toUnixTime(Date date)
  5. toUnixTime(SimpleDateFormat simpleDateFormat, String audioboxDate)
  6. unixTimeToDate(long unixTime)
  7. unixTimeToDateInt(long unixTime)
  8. unixTimeToString(int time)