Java实现时间与字符串互相转换详解

2025-04-02 15:50

本文主要是介绍Java实现时间与字符串互相转换详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下...

一、日期格式化为字符串

Java中,可以使用java.time包中的DateTimeFormatter类将日期格式化为字符串。以下是使用DateTimeFormatter的示例:

(一)使用预定义格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateFormatExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        String formattedDate = now.format(formatter);
        System.out.println("ISO格式日期时间: " + formattedDate);
    }
}

(二)自定义格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CustomFormatExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = now.format(formatter);
        System.out.println("自定义格式日期时间: " + formattedDate);
    }
}

二、字符串解析为日期

将字符串解析为日期时,需要确保字符串的格式与DateTimeFormatter指定的格式一致。以下是解析字符串为日期的示例:

(一)解析ISO格式字符串

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ParseISODateExample {
   China编程 public static void main(String[] args) {
        String isoDate = "2023-10-11T12:34:56";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime date = LocalDateTime.parse(isoDate, formatter);
        System.out.println("解析后的日期时间: " + date);
    }
}

(二)解析自定义格式字符串

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ParseCustomDateExample {
    public static void main(String[] args) {
        String customDate = "2023-10-11 12:34:56";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date = LocalDateTime.parse(customDate, formatter);
        System.out.println("解析后的日期时间: " + date);
    }
}

三、处理不同时区的日期

在处理不同时区的日期时,可以使用ZonedDateTime类。以下是将日期转换为不同时区的示例:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

​​​​​​​public class TimeZoneExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        ZoneId zoneId = ZoneId.of("America/New_York");
        ZonedDateTime zonedDateTime = ZonedDateTime.of(now, zoneI编程China编程d);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
        String formattedDate = zonedDateTime.format(formatter);
        System.out.println("纽约时区日期时间: " + formattedDate);
    }
}

四、总结

Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期和字符串之间进行转换。

五、方法补充

java 时间转字符串

要将Java中的时间转换为字符串,可以使用java.util.Date类或java.time.LocalDateTime类来表示时间,并使用java.text.SimpleDateFormat类来格式化时间。

以下是使用java.util.Date和java.text.SimpleDateFormat的示例代码:

import java.util.Date;
import java.text.SimpleDateFormat;
 
public class Main {
    public static void main(String[] args) {
        Date currentDate = new Date();
 
        // 指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        // 将Date转换为字符串
        String dateString = dateFormat.format(currentDate);
 
        // 打印结果
        System.out.println(dateString);
    }
}

以下是使用java.time.LocalDateTime和java.time.format.DateTimeFormatter的示例代码:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
public class Main {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();
 
        // 指定日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
        // 将LocalDateTime转换为字符串
        String dateTimeString = currentDateTime.format(formatter);
 
        // 打印结果
        System.out.println(dateTimeString);
    }
}

以上示例中的日期格式为"yyyy-MM-dd HH:mm:ss",你可以根据需要自定义日期格式。

java 时间与字符串之间的转换

1. long字符串转换成yyyy-MM-dd HH:mm:ss格式输出

import java.text.SimpleDateFormat;  
import java.util.Date;  
//将long字符串转换成格式时间输出  
public class LongToString {  
public static void main(String argsp[]){  
    String time="125600610php5375";  
  
    Date date=new Date(Long.parseLong(time));  
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    time=formatter.fohttp://www.chinasem.cnrmat(date);  
    System.out.println(time);  
}  
}  

2. 字符串转换成时间

import java.text.SimpleDateFormat;  
import java.util.Date;  
  
import ognl.ParseException;  
  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
    String time="2010-11-20 11:10:10";  
  
    Date date=null;  
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    date=formatter.parse(time);  
    System.out.println(date);  
}  
}  

3. 取得当前系统时间,返回ywww.chinasem.cnyyy-MM-dd HH:mm:ss字符串

import java.text.SimpleDateFormat;  
import java.util.Date;  
  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
      
    Date date=new Date();  
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    String time=formatter.format(date);  
    System.out.println(time);  
}  
}  

4. 取得当前系统时间,返回 HH:mm:ss字符串

import java.text.SimpleDateFormat;  
import java.util.Date;  
  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
      
    Date date=new Date();  
    SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");  
    String time=formatter.format(date);  
    System.out.println(time);  
}  
}  

5.将20101125102503转换成2010-11-25 10:25:03输出

import java.text.SimpleDateFormat;  
import java.util.Date;  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
    String time="20101125102503";  
    SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");  
    SimpleDateFormat formatter2=new SimpleDateFormat("yyyyHHddHHmmss");  
    time=formatter1.format(formatter2.parse(time));  
    System.out.println(time);  
}  
}  

到此这篇关于Java实现时间与字符串互相转换详解的文章就介绍到这了,更多相关Java时间与字符串互转内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!

这篇关于Java实现时间与字符串互相转换详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:http://www.cppcns.com/ruanjian/java/706354.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1154055

相关文章

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定