Java Long Number to Date formatDateTime(long d)

Here you can find the source of formatDateTime(long d)

Description

Convert date and time to string like "yyyy-MM-dd HH:mm".

License

Apache License

Declaration

public static String formatDateTime(long d) 

Method Source Code

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

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

public class Main {
    private static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm";
    private static final int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    /**/*from   w ww.ja va 2s.  com*/
     * Convert date and time to string like "yyyy-MM-dd HH:mm".
     */
    public static String formatDateTime(Date d) {
        return new SimpleDateFormat(DATETIME_FORMAT).format(d);
    }

    /**
     * Convert date and time to string like "yyyy-MM-dd HH:mm".
     */
    public static String formatDateTime(long d) {
        return new SimpleDateFormat(DATETIME_FORMAT).format(d);
    }

    public static String format(int hour, int minute, String format) {
        if ("HH-mm".equals(format)) {
            StringBuilder sb = new StringBuilder(5);
            if (hour < 10)
                sb.append('0');
            sb.append(hour).append(':');
            if (minute < 10)
                sb.append('0');
            return sb.append(minute).toString();
        }
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
        return new SimpleDateFormat(format).format(c.getTime());
    }

    public static String format(int year, int month, int day, String format) {
        if ("MM-dd".equals(format)) {
            StringBuilder sb = new StringBuilder(5);
            return sb.append(month).append('-').append(day).toString();
        }
        if ("yyyy-MM-dd".equals(format)) {
            StringBuilder sb = new StringBuilder(10);
            return sb.append(year).append('-').append(month).append('-').append(day).toString();
        }
        Calendar c = getCalendar(year, month, day);
        return new SimpleDateFormat(format).format(c.getTime());
    }

    /**
     * Return day's year, month(1-12), day(1-31), week(0-6, for SUN, MON, ... SAT), hour, minute, second.
     */
    public static int[] getTime(long t) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(t);
        return getTime(c);
    }

    /**
     * Return day's year, month(1-12), day(1-31), week(0-6, for SUN, MON, ... SAT), hour, minute, second.
     */
    public static int[] getTime(Calendar c) {
        int week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 0)
            week = 7;
        return new int[] { c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), week,
                c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND) };
    }

    /**
     * Get Calendar instance by specified year, month and day.
     * 
     * @param year 4-digit year.
     * @param month Month range is 1-12.
     * @param day Day range is 1-?, end depends on year and month.
     * @return A Calendar instance.
     */
    public static Calendar getCalendar(int year, int month, int day) {
        if (year < 2000 || year > 2100)
            throw new IllegalArgumentException();
        if (month < 1 || month > 12)
            throw new IllegalArgumentException();
        if (day < 1)
            throw new IllegalArgumentException();
        if (month == 2 && isLeapYear(year)) {
            if (day > 29)
                throw new IllegalArgumentException();
        } else {
            if (day > DAYS[month - 1])
                throw new IllegalArgumentException();
        }
        month--;
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    public static boolean isLeapYear(int year) {
        if (year % 100 == 0) {
            return year % 400 == 0;
        }
        return year % 4 == 0;
    }
}

Related

  1. formatDateHeader(final long date)
  2. formatDateHeader(final long date)
  3. formatDateHeader(long millis)
  4. formatDateHeader(long value)
  5. formatDateISO(long millis)
  6. formatDateTime(long d)
  7. formatDateTime(long date)
  8. formatDateTime(long millis)
  9. formatDateTime(long time)