如何在 Java 中把日期转换为字符串
本文介绍了如何在 Java 中把 java.util.Date
转换为 String,并列举了一些示例代码来理解它。
Java 有几个类和方法可以帮助将 Date
转换为 String
,比如使用 SimpleDateFormat
、DateFormatUtils
和 DateTimeFormatter
类。
在 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 中使用 DateTimeFormatter
将 Date
转换为 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
相关文章
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()方法提取年份和月份。
如何检查 NaN 是否存在于 Pandas DataFrame 中
发布时间:2024/04/23 浏览次数:208 分类:Python
-
我们可以使用 isnull()和 isna()方法检查 Pandas DataFrame 中是否存在 NaN。
如何在 Pandas DataFrame 的列中将所有 NaN 值替换为零
发布时间:2024/04/23 浏览次数:198 分类:Python
-
在 Pandas 库中使用 df.fillna(),df.replace()方法在 DataFrame 中将 NaN 值替换为零
如何在 Pandas 中更改列的数据类型
发布时间:2024/04/23 浏览次数:183 分类:Python
-
本教程介绍了如何通过使用 to_numaric,as_type 和 infer 对象来更改 Pandas 中列的数据类型。
如何对 Pandas 中的 DataFrame 行随机排序
发布时间:2024/04/23 浏览次数:128 分类:Python
-
我们可以使用 sample(),shuffle()和 permutation()方法随机地对 Pandas 中的 DataFrame 行进行随机排序。
如何获取 Pandas DataFrame 的行数
发布时间:2024/04/23 浏览次数:71 分类:Python
-
本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。