暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Java时间和时间戳的相互转换

原创 王三又 2020-12-29
2113

/*

 * 将时间转换为时间戳

 */    

public static String dateToStamp(String s) throws ParseException{

    String res;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date date = simpleDateFormat.parse(s);

    long ts = date.getTime();

    res = String.valueOf(ts);

    return res;

}

/*

 * 将时间戳转换为时间

 */

public static String stampToDate(String s){

    String res;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    long lt = new Long(s);

    Date date = new Date(lt);

    res = simpleDateFormat.format(date);

    return res;

}

Java时间和时间戳的相互转换

时间转换为时间戳:

复制代码
/*
* 将时间转换为时间戳
*/
public static String dateToStamp(String s) throws ParseException{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
复制代码
时间戳转换为时间:

复制代码
/*
* 将时间戳转换为时间
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
转载于:https://www.cnblogs.com/ylht/p/10197295.html

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论