java 时间戳转时间 指定时间转时间戳 时间戳转日期 指定日期转时间戳
先看效果:
源码:
1 package com.lgq.common; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class timeTest { 7 public static void main(String[] args) { 8 9 // 时间戳转时间: 10 long timel = 1520821882000L; // 时间戳 11 System.out.println("时间戳转时间:" + toTime(timel)); 12 13 //时间转时间戳: 14 String time = "2018-03-12 10:31:22"; 15 System.out.println("时间转时间戳:" + toTimes(time)); 16 17 } 18 19 /** 20 * 时间戳转时间: 21 * 22 * @param timestamp 23 * @return 24 */ 25 public static String toTime(long timestamp) { 26 Date date = new Date(timestamp); 27 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 28 return sdf.format(date); 29 } 30 31 /** 32 * 时间转时间戳: 33 * 34 * @return 35 */ 36 public static Long toTimes(String time) { 37 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 38 Date date; 39 try { 40 date = sdf.parse(time); 41 return date.getTime(); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 return null; 46 } 47 48 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2022-09-15 【逻辑】【java基础】java 删除逻辑处理 【思维逻辑】【简单明了】