迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

如何在 Java 中把日期转换为字符串

作者:迹忆客 最近更新:2023/03/28 浏览次数:

本文介绍了如何在 Java 中把 java.util.Date 转换为 String,并列举了一些示例代码来理解它。

Java 有几个类和方法可以帮助将 Date 转换为 String,比如使用 SimpleDateFormatDateFormatUtilsDateTimeFormatter 类。


在 Java 中使用 SimpleDateFormat 将日期 Date 转换为字符串 String

这里,我们使用 SimpleDateFormat 类的 format() 方法从 Java 中的 util.Date 对象获取字符串 String

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleTesting {

	public static void main(String[] args) throws ParseException {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date date = new Date();        
		String dateToStr = dateFormat.format(date);
		System.out.println("Date is "+ dateToStr);
	}
}

输出:

Date is 21/09/2020 08:48:01

使用 Java 中的 DateFormatUtils 类将日期 Date 转换为字符串 String

如果你使用的是 Apache 库,那么使用 DateFormateUtils 类的 format() 方法。它在 Java 中把 java.util.Date 转换为字符串后返回一个字符串。

import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;

public class SimpleTesting {

	public static void main(String[] args) throws ParseException {
		Date date = new Date();        
		String dateToStr = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:SS");
		System.out.println("Date is "+ dateToStr);
	}
}

输出:

Date is 2020-09-21 08:51:203

在 Java 中使用 DateTimeFormatterDate 转换为 String

在这里,我们使用 format() 方法,它将 ofPattern() 方法作为参数,并返回日期的字符串表示。请看下面的例子。

import java.text.ParseException;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class SimpleTesting {

	public static void main(String[] args) throws ParseException {
		Date date = new Date();        
		String dateToStr = date.toInstant()
				.atOffset(ZoneOffset.UTC)
				.format( DateTimeFormatter.ofPattern("dd-MM-yyyy"));
		System.out.println("Date is "+ dateToStr);
	}
}

输出:

Date is 21-09-2020

在 Java 中将日期 Date 转换为带有时区信息的字符串 String

这里,我们使用 DateTimeFormatter 类的 format() 方法从 java.util.date 转换后得到字符串。因为我们在 ofPattern() 方法中指定了日期-时间格式,所以我们得到了时区和日期。

import java.text.ParseException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class SimpleTesting {

	public static void main(String[] args) throws ParseException {
		Date date = new Date();        
		DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS Z")
				.withZone(ZoneId.systemDefault());
		String dateToStr = format.format(date.toInstant()); 
		System.out.println("Date is "+ dateToStr);
	}
}

输出:

Date is 2020-09-21 09:10:23:991 +0530

用 Java 中的 String 类将日期 Date 转换为字符串 String

这是一个最简单的解决方案,以获得一个 java.util.date 对象的字符串。在这里,我们使用 String 类的 format() 方法,根据指定的格式来格式化日期。请看下面的例子。

import java.text.ParseException;
import java.util.Date;

public class SimpleTesting {

	public static void main(String[] args) throws ParseException {
		Date date = new Date();        
		String dateToStr = String.format("%1$tY-%1$tm-%1$td", date);
		System.out.println("Date is "+ dateToStr);
	}
}

输出:

Date is 2020-09-21

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

如何从 Pandas 的日期时间列中提取月份和年份

发布时间:2024/04/23 浏览次数:160 分类:Python

我们可以分别使用 dt.year()和 dt.month()方法从 Datetime 列中提取出年和蛾。我们还可以使用 pandas.DatetimeIndex.month 以及 pandas.DatetimeIndex.year 和 strftime()方法提取年份和月份。

如何获取 Pandas DataFrame 的行数

发布时间:2024/04/23 浏览次数:71 分类:Python

本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便