Android 时间戳简单转化

来源:互联网 发布:触摸屏软件下载 编辑:程序博客网 时间:2024/06/05 01:18

–转自http://www.cnblogs.com/zhujiabin/p/4238084.html

时间戳就是如1377216000000 这种格式我们在mysql数据库中会经常用到把时间转换成时间戳或把时间戳转换成日期格式了,下面我来介绍安卓中时间戳操作转换方法。

一、原理

时间戳的原理是把时间格式转为十进制格式,这样就方便时间的计算。好~ 直接进入主题。(下面封装了一个类,有需要的同学可以参考或是直接Copy 就可以用了。)
如: 2013年08月23日 转化后是 1377216000000

二、步骤

1、创建 DateUtilsl类,我对原版的代码稍微进行了些修改

public class DateUtils {    private static SimpleDateFormat mSimpleDateFormat = null;    //获取系统时间    public static String getCurrentDate() {        Date d = new Date();        mSimpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒", Locale.getDefault());        return mSimpleDateFormat.format(d);    }    /*时间戳转换成字符窜*/    public static String getDateToString(long time) {        Date d = new Date(time);        mSimpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒", Locale.getDefault());        return mSimpleDateFormat.format(d);    }    /*将字符串转为时间戳*/    public static long getStringToDate(String time) {        mSimpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒", Locale.getDefault());        Date date = new Date();        try {            date = mSimpleDateFormat.parse(time);        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return date.getTime();    }}

2、在对应使用的地方调用就可以了。

DateUtils.getCurrentDate(); //获取系统当前时间DateUtils.getDateToString(时间戳); //时间戳转为时间格式DateUtils.getStringToDate("时间格式");//时间格式转为时间戳
0 0
原创粉丝点击