Android中获取时间戳及时间戳的转换

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42618969/article/details/81364007
//获取当前时间戳
long timeStamp = System.currentTimeMillis();
String time = stampToDate(timeStamp);
Log.d("xxxxx", time);
//获取当前时间
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
Log.d("xxxxx",year + "/" + (month+1) + "/" + date + " " +hour + ":" +minute + ":" + second);
/*
 * 将时间转换为时间戳
 */
public String dateToStamp(String time) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = simpleDateFormat.parse(time);
    long ts = date.getTime();
    return String.valueOf(ts);
}

/*
 * 将时间戳转换为时间
 */
public String stampToDate(long timeMillis){
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(timeMillis);
    return simpleDateFormat.format(date);
}
展开阅读全文

没有更多推荐了,返回首页