Android 获取系统时间及时间戳转换

//获取当前时间戳
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);
}

猜你喜欢

转载自blog.csdn.net/qq_25749749/article/details/80677468