
Java
1. 获取当前时间
使用Java类库中的Date类来获取当前的日期时间:

AI
Date date = new Date();
2. 获取当前时间戳
使用System类中的currentTimeMillis方法来获取当前时间的时间戳:
long timestamp = System.currentTimeMillis();
3. 获取指定格式的日期时间
使用SimpleDateFormat类来指定日期时间的格式,例如:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(new Date());
其中,"yyyy-MM-dd HH:mm:ss"为日期时间的格式化字符串,可以根据需要自行修改。
4. 获取指定时区的日期时间
使用Calendar类来获取指定时区的日期时间,例如:
TimeZone timeZone = TimeZone.getTimeZone("Asia/ShanghAI");
Calendar calendar = Calendar.getInstance(timeZone);
Date date = calendar.getTime();
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号