Java Long Number to Time getShortTimeText(Long oldTime)

Here you can find the source of getShortTimeText(Long oldTime)

Description

get Short Time Text

License

Open Source License

Declaration

@SuppressWarnings("deprecation")
    public static String getShortTimeText(Long oldTime) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    public final static String TimeFormat = "yyyyMMddHHmmss";
    public final static String DisplayDateFormat = "yyyy-MM-dd";

    @SuppressWarnings("deprecation")
    public static String getShortTimeText(Long oldTime) {
        Date time = parseDate(oldTime, TimeFormat);
        Date now = new Date();
        String format = "yyyy-MM-dd HH:mm";
        if (time.getYear() == now.getYear()) {
            format = format.substring("yyyy-".length());
            if (time.getMonth() == now.getMonth() && time.getDay() == now.getDay()) {
                format = format.substring("MM-dd ".length());
            }/*  w  w  w  .j  a  v a2s  .co m*/
        }
        return formatDate(time, format);
    }

    public static Date parseDate(Long date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        try {
            return simpleDateFormat.parse(date + "");
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseDate(String date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        try {
            return simpleDateFormat.parse(date + "");
        } catch (ParseException e) {
            return null;
        }
    }

    public static String formatDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Date date) {
        return formatDate(date, DisplayDateFormat);
    }
}

Related

  1. getExpires(long maxAge)
  2. getHMSFromMills(long millSec)
  3. getOldCookieDate(long time)
  4. getReadableDate(long longTime)
  5. getServerTime(long offset)
  6. getSimpleDate(long time)
  7. getSMillon(long time)
  8. getSpeedString(long castTime, long bytes)
  9. getStrDateForLong(long time)