Products
GG网络技术分享 2025-11-13 06:56 1
在Java中,将字符串时候戳转换为时候以及将时候戳转换回字符串格式是一个常见的操作。SimpleDateFormat和Date类来实现这些个转换。
SimpleDateFormat对象,指定目标日期格式。SimpleDateFormat的parse方法将字符串转换为Date对象。Date对象的getTime方法获取时候戳。java import java.text.SimpleDateFormat; import java.util.Date;

public class TimestampConversion { public static void main { try { String strDate = "2018-01-09 12:17:22"; SimpleDateFormat sdf = new SimpleDateFormat; Date date = sdf.parse; long timestamp = date.getTime; System.out.println; } catch { e.printStackTrace; } } }
Date对象,传入时候戳。SimpleDateFormat对象,指定目标日期格式。SimpleDateFormat的format方法将Date对象格式化为字符串。public class TimestampConversion { public static void main { long timestamp = 1632086400000L; // 示例时候戳 Date date = new Date; SimpleDateFormat sdf = new SimpleDateFormat; String strDate = sdf.format; System.out.println; } }
SimpleDateFormat是非线程平安的, Ru果需要在许多线程周围中用,应考虑用ThreadLocal或java.time.format.DateTimeFormatter。Demand feedback