时间戳转成年月日

public class TimeUtils {

    public static String timeFormat(long timeMillis, String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.CHINA);
        return format.format(new Date(timeMillis));
    }

    public static String formatPhotoDate(long time){
        return timeFormat(time, "yyyy-MM-dd");
    }

    public static String formatPhotoDate(String path){
        File file = new File(path);
        if(file.exists()){
            long time = file.lastModified();
            return formatPhotoDate(time);
        }
        return "1970-01-01";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
时间戳转成年月日是一个非常常见的需求,在使用 JavaScript 进行开发时也经常会遇到。ES6 中提供的 Date 对象可以非常方便地实现时间戳转成年月日。 在 ES6 中,我们可以使用 Date 类型的 from(timestamp) 方法,将时间戳换为 Date 类型的对象。基于该 Date 对象,我们可以使用 Date 类型的实例方法,如 getFullYear()、getMonth() 和 getDate(),便捷地获取年份、月份和日期。 下面是一个实现时间戳转成年月日的示例代码: ```javascript const timestamp = 1625681232000; // 时间戳 const date = new Date(timestamp); // 创建 Date 对象 const year = date.getFullYear(); // 获取年份 const month = date.getMonth() + 1; // 获取月份 const day = date.getDate(); // 获取日期 const formattedDate = `${year}-${month}-${day}`; // 格式化日期 console.log(formattedDate); // 输出:2021-7-7 ``` 在上述示例代码中,我们先定义一个时间戳变量,然后使用 new Date(timestamp) 创建了一个 Date 对象。接着,我们分别使用 getFullYear()、getMonth() 和 getDate() 实例方法获取了年份、月份和日期,并将其保存在对应的变量中。最后,我们使用 ES6 中的字符串模板,将年月日格式化成了指定的字符串格式。 通过上述示例,我们可以发现,ES6 中实现时间戳转成年月日非常简单,并且代码可读性也很好。因此,在实际开发中,我们可以大胆使用 ES6 提供的语法糖,提高开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值