时间戳是一种表示时间的方式,它表示从1970年1月1日00:00:00到当前时间的秒数。在Java中,我们经常需要将这种时间戳转换成具体的日期和时间,或将日期和时间转换成时间戳。本文将详细介绍如何在Java中进行时间戳转换。
一、将时间戳转换成日期和时间
Java中可以使用Date类来表示具体的日期和时间,我们可以使用new Date(timestamp)方法将时间戳转换成Date对象,其中timestamp为要转换的时间戳。
示例代码如下:
long timestamp = System.currentTimeMillis(); //获取当前时间戳
Date date = new Date(timestamp); //将时间戳转换成Date对象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
String datetime = dateFormat.format(date); //将Date对象格式化成字符串
System.out.println(datetime); //输出格式化后的日期和时间
在上述示例代码中,我们首先通过System.currentTimeMillis()方法获取当前时间戳,然后将其转换成Date对象,接着使用SimpleDateFormat类设置日期和时间的格式,最后将Date对象格式化成字符串并输出。
输出结果如下:
2021-05-24 18:20:43
二、将日期和时间转换成时间戳
与将时间戳转换成日期和时间相反,将日期和时间转换成时间戳需要使用System.currentTimeMillis()方法,该方法可以获取当前系统的时间戳。
示例代码如下:
String datetime = "2021-05-24 18:20:43"; //要转换的日期和时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
Date date = dateFormat.parse(datetime); //将字符串解析成Date对象
long timestamp = date.getTime(); //将Date对象转换成时间戳
System.out.println(timestamp); //输出时间戳
在上述示例代码中,我们首先定义要转换的日期和时间,然后设置日期和时间的格式,接着使用SimpleDateFormat类将字符串解析成Date对象,最后将Date对象转换成时间戳并输出。
输出结果如下:
1621854043000
三、将时间戳转换成本地时间
默认情况下,Java会将时间戳转换成UTC时间,而不是本地时间。如果需要将时间戳转换成本地时间,可以使用Calendar.getInstance()方法获取Calendar对象,并调用setTimeInMillis(timestamp)方法将时间戳设置到Calendar对象中,然后调用get()方法获取年、月、日、时、分、秒等属性值。
示例代码如下:
long timestamp = System.currentTimeMillis(); //获取当前时间戳 Calendar calendar = Calendar.getInstance(); //获取Calendar对象 calendar.setTimeInMillis(timestamp); //将时间戳设置到Calendar对象中 int year = calendar.get(Calendar.YEAR); //获取年份 int month = calendar.get(Calendar.MONTH) + 1; //获取月份(需要加1) int day = calendar.get(Calendar.DAY_OF_MONTH); //获取日 int hour = calendar.get(Calendar.HOUR_OF_DAY); //获取小时 int minute = calendar.get(Calendar.MINUTE); //获取分钟 int second = calendar.get(Calendar.SECOND); //获取秒钟 System.out.println(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second); //输出本地时间
在上述示例代码中,我们首先获取当前时间戳,然后使用Calendar.getInstance()方法获取Calendar对象,将时间戳设置到Calendar对象中,最后使用get()方法获取年、月、日、时、分、秒等属性值。
输出结果如下:
2021-5-24 18:20:43
四、本文代码示例
本文中所示的完整的代码示例如下:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtils {
//将时间戳转换成日期和时间
public static String timestampToDate(long timestamp) {
Date date = new Date(timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
//将日期和时间转换成时间戳
public static long dateToTimestamp(String datetime) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse(datetime);
return date.getTime();
}
//将时间戳转换成本地时间
public static String timestampToLocalTime(long timestamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
//测试代码
public static void main(String[] args) throws Exception {
long timestamp = System.currentTimeMillis();
String datetime = timestampToDate(timestamp);
System.out.println("时间戳转换成日期和时间:" + datetime);
long newTimestamp = dateToTimestamp(datetime);
System.out.println("日期和时间转换成时间戳:" + newTimestamp);
String localTime = timestampToLocalTime(timestamp);
System.out.println("时间戳转换成本地时间:" + localTime);
}
}
在本文中,我们使用TimeUtils类封装了时间戳转换的功能,并使用main()方法进行测试。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/248359.html
微信扫一扫
支付宝扫一扫