java如何看时间
获取当前时间
使用 java.time.LocalDateTime 或 java.util.Date 可以获取当前时间。以下是示例代码:
import java.time.LocalDateTime;
import java.util.Date;
public class GetCurrentTime {
public static void main(String[] args) {
// 使用 java.time.LocalDateTime(Java 8及以上推荐)
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("当前时间 (LocalDateTime): " + currentTime);
// 使用 java.util.Date(旧版兼容)
Date date = new Date();
System.out.println("当前时间 (Date): " + date);
}
}
格式化时间输出
使用 java.time.format.DateTimeFormatter 或 java.text.SimpleDateFormat 可以自定义时间格式:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FormatTime {
public static void main(String[] args) {
// 使用 DateTimeFormatter(Java 8及以上)
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedTime = now.format(formatter);
System.out.println("格式化时间 (DateTimeFormatter): " + formattedTime);
// 使用 SimpleDateFormat(旧版兼容)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(new Date());
System.out.println("格式化时间 (SimpleDateFormat): " + formattedDate);
}
}
获取时间戳
时间戳是从1970年1月1日(UTC)开始的毫秒数:
public class GetTimestamp {
public static void main(String[] args) {
long timestamp = System.currentTimeMillis();
System.out.println("当前时间戳 (毫秒): " + timestamp);
// 转换为秒
long timestampInSeconds = timestamp / 1000;
System.out.println("当前时间戳 (秒): " + timestampInSeconds);
}
}
解析字符串为时间
将字符串转换为时间对象:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseTime {
public static void main(String[] args) throws ParseException {
// 使用 DateTimeFormatter(Java 8及以上)
String timeStr = "2023-10-05 14:30:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parsedTime = LocalDateTime.parse(timeStr, formatter);
System.out.println("解析后的时间 (LocalDateTime): " + parsedTime);
// 使用 SimpleDateFormat(旧版兼容)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = sdf.parse(timeStr);
System.out.println("解析后的时间 (Date): " + parsedDate);
}
}
时间加减操作
使用 java.time 包可以方便地进行时间加减:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class TimeCalculation {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// 加1天
LocalDateTime tomorrow = now.plusDays(1);
System.out.println("明天: " + tomorrow);
// 减2小时
LocalDateTime twoHoursAgo = now.minusHours(2);
System.out.println("两小时前: " + twoHoursAgo);
// 加30分钟
LocalDateTime after30Minutes = now.plus(30, ChronoUnit.MINUTES);
System.out.println("30分钟后: " + after30Minutes);
}
}
时区处理
如果需要处理时区,可以使用 ZonedDateTime:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class TimeZoneExample {
public static void main(String[] args) {
// 获取当前时区时间
ZonedDateTime zonedNow = ZonedDateTime.now();
System.out.println("当前时区时间: " + zonedNow);
// 转换为其他时区(如纽约)
ZonedDateTime newYorkTime = zonedNow.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println("纽约时间: " + newYorkTime);
// 格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
System.out.println("格式化时区时间: " + newYorkTime.format(formatter));
}
}
以上方法涵盖了Java中处理时间的常见需求,推荐使用Java 8引入的 java.time 包,因为它更现代且功能更强大。






