当前位置:天才代写 > tutorial > 安卓教程 > android 时间戳相关源码

android 时间戳相关源码

2021-02-18 12:47 星期四 所属: 安卓教程 浏览:11

 /**
     * java时间格式是13位的,php后台管理是10位的,因此 要断开
     */
    private static long timestampConvent(long stamp) {
        String temp = stamp   "";
        return Long.parseLong(temp.substring(0, 10));
    }


 /**
     * 获得今日的时间格式
     */
    public static long getNowTimeStamp() {
        return timestampConvent(new Date().getTime());
    }

/**
     * 获得明日的时间格式
     */
    public static long getTomorrowTimeStamp() {
        Calendar tomrrow = Calendar.getInstance();
        tomrrow.add(Calendar.DAY_OF_MONTH, 1);
        return timestampConvent(tomrrow.getTime().getTime());
    }

/**
     * 获得特定時间的时间格式
     */
    public static long getTimeStamp(String time, String format) {
        Date date;
        //留意format的文件格式要与日期String的文件格式相符合
        DateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        try {
            date = sdf.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return timestampConvent(date.getTime());
    }


/**
     * 把时间格式变为阅读文章友善的字符串数组
     *
     * @param time 10位时间格式,由于服务端传出去的是10位,因此 在android里边应用时必须*1000提升到13位
     */
    public static String getFormatTime(long time) {
        Calendar target = Calendar.getInstance();
        target.setTime(new Date(time * 1000));

        String format = "yyyy年MM月dd日 HH:mm:ss";
        Calendar today = Calendar.getInstance();

        if (today.get(Calendar.YEAR) == target.get(Calendar.YEAR) &&
                today.get(Calendar.MONTH) == target.get(Calendar.MONTH) &&
                today.get(Calendar.DAY_OF_MONTH) == target.get(Calendar.DAY_OF_MONTH)) {
            //今日
            if (target.get(Calendar.HOUR_OF_DAY) < 3) {
                format = "零晨 HH:mm:ss";
            } else if (target.get(Calendar.HOUR_OF_DAY) < 12) {
                format = "早上 HH:mm:ss";
            } else if (target.get(Calendar.HOUR_OF_DAY) < 18) {
                format = "中午 HH:mm:ss";
            } else {
                format = "夜里 HH:mm:ss";
            }
        } else {
            target.add(Calendar.DAY_OF_MONTH, 1);//加1,假如時间为昨日得话,加1以后的时间就是今日了

            if (today.get(Calendar.YEAR) == target.get(Calendar.YEAR) &&
                    today.get(Calendar.MONTH) == target.get(Calendar.MONTH) &&
                    today.get(Calendar.DAY_OF_MONTH) == target.get(Calendar.DAY_OF_MONTH)) {
                //昨日
                format = "昨日 HH:mm:ss";
            } else {
                target.setTime(new Date(time * 1000));

                if (target.get(Calendar.YEAR) == today.get(Calendar.YEAR)) {
                    format = "MM月dd日 HH:mm:ss";
                }
            }
        }
        return StaticMethod.timestampToString(time   "", format);
    }

/**
     * 10位时间戳转换为特定文件格式的時间字符串数组
     */
    public static String timestampToString(String time, String format) {
        if (time.length() > 3) {
            long temp = Long.parseLong(time) * 1000;
            Timestamp ts = new Timestamp(temp);
            String tsStr = "";
            DateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
            try {
                tsStr = dateFormat.format(ts);
                System.out.println(tsStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return tsStr;
        } else {
            return "";
        }
    }

  

 

    关键字:

天才代写-代写联系方式