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

版权声明:独学而无友,则孤陋寡闻。q群582951247 https://blog.csdn.net/mp624183768/article/details/79945116
//获取当前时间戳
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);
}
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

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