Java Time Format printDate(final long time, final String dateFormat)

Here you can find the source of printDate(final long time, final String dateFormat)

Description

print Date

License

Open Source License

Parameter

Parameter Description
time a date/time in seconds since the UNIX epoch.
dateFormat the date/time format string

Return

the date/time formatted as a String

Declaration

public static String printDate(final long time, final String dateFormat) 

Method Source Code

//package com.java2s;
/*//w  w  w  .java2s .  c om
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

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

public class Main {
    /**
     * Difference in second between 01/01/1970 00:00:00 (java reference time)
     * and 01/01/1904 00:00:00 (HFS reference time).
     */
    public static final long MAC_DATE_CONVERSION = 2082844800L;

    /**
     * 
     * @param time a date/time in seconds since the UNIX epoch.
     * @param dateFormat the date/time format string
     * @return the date/time formatted as a String
     */
    public static String printDate(final long time, final String dateFormat) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(getDate(time, false) * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(cal.getTime());
    }

    /**
     * Convert time from/to java time to/from mac time.
     * 
     * @param time in seconds since reference date.
     * @param encode if {code true}, convert from java to mac, otherwise convert
     *            from mac to java.
     * 
     * @return the converted time
     */
    public static long getDate(long time, boolean encode) {
        time = (encode) ? time + MAC_DATE_CONVERSION : time
                - MAC_DATE_CONVERSION;
        return time;
    }
}

Related

  1. isDateFormat(String timeString)
  2. long2Datestr(long time, String format)
  3. long2String(long longTime, String dataFormat)
  4. long2String(long longTime, String dataFormat)
  5. newDateTimeFormat()
  6. printFormatDate(long dateTime)
  7. setDateTimeFormat(String format)
  8. stdTimeFormat(Date date)
  9. string2Long(String sourceTime, String dataFormat)