Print date time by sys Time and date Time - Android java.util

Android examples for java.util:Date

Description

Print date time by sys Time and date Time

Demo Code

/*/*w ww  .j  av a  2 s .c  o m*/
 * Copyright (C) 2016 venshine.cn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{
    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss:ms";
    public static final int DAY = 24 * 60 * 60 * 1000;
    /**
     * Print date time by sysTime and dateTime
     *
     * @param sysTime  server time
     * @param dateTime publish time
     * @return
     */
    public static String printDate(long sysTime, long dateTime) {
        long min = 60 * 1000;
        long hour = 60 * min;
        if (sysTime - dateTime <= hour) { // one hour
            if (sysTime - dateTime <= min) { // one minute
                return "just now";
            }
            return ((sysTime - dateTime) / min) + "minutes ago";
        }
        if (isToday(sysTime, dateTime)) {// today
            SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");
            Date date1 = new Date(dateTime);
            return "today " + sdf1.format(date1);
        }
        if (isYesterday(sysTime, dateTime)) {// yesterday
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
            Date date2 = new Date(dateTime);
            return "yesterday " + sdf2.format(date2);
        }
        // other?MM-dd hh:mm
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        Date date = new Date(dateTime);
        return sdf.format(date);
    }
    /**
     * Judge whether dateTime is today by sysTime and dateTime
     *
     * @param sysTime  server time
     * @param dateTime publish time
     * @return
     */
    public static boolean isToday(long sysTime, long dateTime) {
        String s = getTime(sysTime, DATE_FORMAT); // 2015-06-15?2015-06-15 14:44:02
        long l = getTime(s, DATE_FORMAT); // 1434297600000? 2015-06-15 00:00:00
        return dateTime - l >= 0;
    }
    /**
     * Judge whether dateTime is yesterday by sysTime and dateTime
     *
     * @param sysTime  server time
     * @param dateTime publish time
     * @return
     */
    public static boolean isYesterday(long sysTime, long dateTime) {
        long yesterday = sysTime - DAY;
        String s = getTime(yesterday, DATE_FORMAT); // 2015-06-14?2015-06-14 14:44:02
        long l = getTime(s, DATE_FORMAT); // 1434297600000? 2015-06-14 00:00:00
        return dateTime - l >= 0;
    }
    /**
     * Get current time, yyyy-MM-dd hh:mm:ss.
     *
     * @return
     */
    public static String getTime() {
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT);
        return df.format(now);
    }
    /**
     * Convert long time to String time
     *
     * @param time
     * @param format
     * @return
     */
    public static String getTime(long time, String format) {
        if (time > 0) {
            if (StringUtils.isEmpty(format)) {
                format = DATE_FORMAT;
            }
            SimpleDateFormat sf = new SimpleDateFormat(format);
            Date date = new Date(time);
            return sf.format(date);
        }
        return "";
    }
    /**
     * Convert String time to long time
     *
     * @param time
     * @param format
     * @return
     */
    public static long getTime(String time, String format) {
        try {
            if (null != time) {
                if (format == null) {
                    format = DATE_FORMAT;
                }
                SimpleDateFormat sf = new SimpleDateFormat(format);
                return sf.parse(time).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

Related Tutorials