当前位置:首页 > Java

java如何看时间

2026-03-17 22:51:15Java

获取当前时间

使用 java.time.LocalDateTimejava.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.DateTimeFormatterjava.text.SimpleDateFormat 可以自定义时间格式:

java如何看时间

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);
    }
}

解析字符串为时间

将字符串转换为时间对象:

java如何看时间

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 包,因为它更现代且功能更强大。

标签: 如何看时间
分享给朋友:

相关文章

vue 实现时间

vue 实现时间

Vue 实现时间的几种方法 在Vue中实现时间显示或处理时间数据,可以通过多种方式实现,包括原生JavaScript、第三方库或Vue插件。以下是几种常见的方法: 使用原生JavaScript显示当…

vue实现时间天气

vue实现时间天气

以下是在Vue中实现时间和天气功能的几种方法: 获取并显示当前时间 使用JavaScript的Date对象获取当前时间,并通过Vue的数据绑定显示: <template> <…

如何看react源码

如何看react源码

阅读React源码的方法 克隆React仓库 从GitHub克隆React的官方仓库,切换到稳定版本分支。React采用Monorepo结构,核心代码在packages/react和packages/…

react如何获取当前时间

react如何获取当前时间

获取当前时间的几种方法 在React中获取当前时间可以通过多种方式实现,以下是常见的几种方法: 使用JavaScript的Date对象 const currentTime = new Date()…

react如何根据时间来分类

react如何根据时间来分类

根据时间分类的实现方法 在React中根据时间分类数据通常涉及日期处理、数据分组和动态渲染。以下是几种常见场景的实现方式: 使用数组reduce方法分组 假设有一组带时间戳的数据,需要按天/月/年分…

react如何改变输入框时间

react如何改变输入框时间

改变输入框时间的方法 在React中处理输入框时间通常涉及使用<input type="time">元素,并通过状态管理来控制其值。以下是几种常见场景的实现方式: 使用受控组件 通过Re…