Popular Posts
android.intent.action.SCREEN_ON & android.intent.action.SCREEN_OFF First, I've tried create a receiver to receive screen on/off and register receiver on AndroidManifest.xml like below, but unfortunately ... Multiple line of text limit With Sharepoint Designer, edit the page of list view. Add xsl template as below to override original template. Source template could be foun... Memo: Debounce Task To prevent multi-execution from caller in short time, use debounce for single execution. var debounce = function (func, threshold, execAsap...
Stats
DateHelper
package bruce.lib;

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

/**
 * @author NaNashi
 * 
 */
public class DateHelper {
    private final static SimpleDateFormat LOG_DATE = new SimpleDateFormat("yyyyMMdd", Locale.US);
    private final static SimpleDateFormat DATE_ONLY = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
    private final static SimpleDateFormat FULL_DATE = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);

    public final static String logDate() {
        return LOG_DATE.format(new Date());
    }

    public final static String formatDate(Date date) {
        return date == null ? null : DATE_ONLY.format(date);
    }

    public final static String formatFullDate(Date date) {
        return date == null ? null : FULL_DATE.format(date);
    }

    public final static Date parseDate(String source) {
        return parseDate(source, false);
    }

    public final static Date parseDate(String source, boolean returnNowOnfailed) {
        try {
            return DATE_ONLY.parse(source);
        } catch (Exception e) {
            return returnNowOnfailed ? new Date() : null;
        }
    }

    public final static Date parseFullDate(String source) {
        return parseDate(source, false);
    }

    public final static Date parseFullDate(String source, boolean returnNowOnfailed) {
        try {
            return FULL_DATE.parse(source);
        } catch (Exception e) {
            return returnNowOnfailed ? new Date() : null;
        }
    }

    public final static String diffTime(Date before, Date after) {
        long sticks = Math.abs(before.getTime() - after.getTime());
        int seconds = (int) (sticks / 1000);
        int minutes = seconds / 60;
        int hours = minutes / 60;
        int days = hours / 24;

        StringBuilder sb = new StringBuilder();
        if (days > 0) {
            sb.append(days).append("天");
            hours -= days * 24;
            minutes -= days * 24 * 60;
            seconds -= days * 24 * 60 * 60;
        }
        if (hours > 0) {
            sb.append(hours).append("小時");
            minutes -= hours * 60;
            seconds -= hours * 60 * 60;
        }
        if (hours > 0 || minutes > 0) {
            sb.append(minutes).append("分");
            seconds -= minutes * 60;
        }

        sb.append(seconds).append("秒").append(before.after(after) ? "後" : "前");
        return sb.toString();
    }

    public static String replaceMonthWord(String s) {
        s = s.toLowerCase();
        String[] month = new String[] { "January", "Februaary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        for (int i = 0; i < month.length; i++) {
            String m = month[i].toLowerCase();
            if (s.contains(m.toLowerCase()))
                return s.replace(m, String.valueOf(i + 1));
        }
        return s;
    }
}