ITPub博客

首页 > 应用开发 > Java > 直播短视频系统,android 时间戳转换

直播短视频系统,android 时间戳转换

原创 Java 作者:zhibo系统开发 时间:2020-11-26 14:05:57 0 删除 编辑

直播短视频系统,android 时间戳转换相关的代码

package util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import network.ParserJson;
public class TimeUtils {
    static String dayNames[] = {ParserJson.getValMap("sunday"), ParserJson.getValMap("monday"), ParserJson.getValMap("tuesday"), ParserJson.getValMap("wednesday"), ParserJson.getValMap("thursday"), ParserJson.getValMap("friday"), ParserJson.getValMap("saturday")};
    public static String getNewChatTime(long timesamp) {
        String result = "";
        Calendar todayCalendar = Calendar.getInstance();
        Calendar otherCalendar = Calendar.getInstance();
        otherCalendar.setTimeInMillis(timesamp);
        String timeFormat="M/d/ HH:mm";
        String yearTimeFormat="yyyy/M/d/ HH:mm";
        String am_pm="";
        int hour=otherCalendar.get(Calendar.HOUR_OF_DAY);
        if(hour>=0&&hour<6){
            //am_pm="凌晨";
        }else if(hour>=6&&hour<12){
           /// am_pm="早上";
        }else if(hour==12){
           // am_pm="中午";
        }else if(hour>12&&hour<18){
           // am_pm="下午";
        }else if(hour>=18){
            //am_pm="晚上";
        }
        timeFormat="M/d "+ am_pm +"HH:mm";
        yearTimeFormat="M/d "+ am_pm +"HH:mm";
        boolean yearTemp = todayCalendar.get(Calendar.YEAR)==otherCalendar.get(Calendar.YEAR);
        if(yearTemp){
            int todayMonth=todayCalendar.get(Calendar.MONTH);
            int otherMonth=otherCalendar.get(Calendar.MONTH);
            if(todayMonth==otherMonth){//表示是同一个月
                int temp=todayCalendar.get(Calendar.DATE)-otherCalendar.get(Calendar.DATE);
                switch (temp) {
                    case 0:
                        result = getHourAndMin(timesamp);
                        break;
                    case 1:
                        result = ParserJson.getValMap("yesterday") + getHourAndMin(timesamp);
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        int dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH);
                        int todayOfMonth=todayCalendar.get(Calendar.WEEK_OF_MONTH);
                        if(dayOfMonth==todayOfMonth){//表示是同一周
                            int dayOfWeek=otherCalendar.get(Calendar.DAY_OF_WEEK);
                            if(dayOfWeek!=1){//判断当前是不是星期日 如想显示为:周日 12:09 可去掉此判断
                                result = dayNames[otherCalendar.get(Calendar.DAY_OF_WEEK)-1] + getHourAndMin(timesamp);
                            }else{
                                result = getTime(timesamp,timeFormat);
                            }
                        }else{
                            result = getTime(timesamp,timeFormat);
                        }
                        break;
                    default:
                        result = getTime(timesamp,timeFormat);
                        break;
                }
            }else{
                result = getTime(timesamp,timeFormat);
            }
        }else{
            result=getYearTime(timesamp,yearTimeFormat);
        }
        return result;
    }
    /**
     * 当天的显示时间格式
     * @param time
     * @return
     */
    public static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }
    /**
     * 不同一周的显示时间格式
     * @param time
     * @param timeFormat
     * @return
     */
    public static String getTime(long time,String timeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        return format.format(new Date(time));
    }
    /**
     * 不同年的显示时间格式
     * @param time
     * @param yearTimeFormat
     * @return
     */
    public static String getYearTime(long time,String yearTimeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
        return format.format(new Date(time));
    }
}

以上就是 直播短视频系统,android 时间戳转换相关的代码, 更多内容欢迎关注之后的文章



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69978258/viewspace-2737165/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-07-02

  • 博文量
    163
  • 访问量
    70204