提问人:littleK 提问时间:11/18/2010 最后编辑:MahozadlittleK 更新时间:12/15/2022 访问量:2412179
Java 字符串到日期的转换
Java string to date conversion
问:
在 Java 中将格式为“January 2, 2010”的 a 转换为 a 的最佳方法是什么?String
Date
最终,我想将月、日和年分解为整数,以便我可以使用
Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();
将日期转换为时间。
答:
这是一条艰难的道路,自 Java 1.1(1997 年)以来,这些 setter 方法已被弃用。此外,自从 Java 8(2014 年)引入 API 以来,整个类实际上已被弃用(不推荐)。java.util.Date
java.util.Date
java.time
只需使用 DateTimeFormatter
格式化日期,并使用与输入字符串匹配的模式(此处提供教程)。
在将“2010 年 1 月 2 日”作为输入字符串的特定情况下:
- “一月”是全文月份,因此请使用该模式
MMMM
- “2”是一个月中的短日,因此请使用该模式。
d
- “2010”是 4 位数的年份,因此请使用该模式。
yyyy
String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02
注意:如果你的格式模式恰好也包含时间部分,那么使用 LocalDateTime#parse(text, formatter) 而不是 LocalDate#parse(text, formatter
)。
而且,如果您的格式模式恰好也包含时区,请改用 ZonedDateTime#parse(text, formatter)。
下面是从 javadoc 中提取的相关性,列出了所有可用的格式模式:
象征 | 意义 | 介绍 | 例子 |
---|---|---|---|
G |
时代 | 发短信 | 广告;公元;一个 |
u |
年 | 年 | 2004;04 |
y |
年份 | 年 | 2004;04 |
D |
一年中的某一天 | 数 | 189 |
M /L |
一年中的月份 | 数字/文本 | 7;07;七月;七月;J |
d |
月中的某一天 | 数 | 10 |
Q /q |
季度 | 数字/文本 | 3;03;问题3;第三季度 |
Y |
基于周的年份 | 年 | 1996;96 |
w |
基于周的一年 | 数 | 27 |
W |
月度周 | 数 | 4 |
E |
星期几 | 发短信 | 星期二;星期二;T |
e /c |
本地化的星期几 | 数字/文本 | 2;02;星期二;星期二;T |
F |
月度周 | 数 | 3 |
a |
每日上午至下午上午 | 发短信 | 下午 |
h |
时钟-上午-下午时间(1-12) | 数 | 12 |
K |
凌晨时段 (0-11) | 数 | 0 |
k |
时钟-上午-下午一小时 (1-24) | 数 | 0 |
H |
一天中的一小时 (0-23) | 数 | 0 |
m |
分钟整点 | 数 | 30 |
s |
分钟秒 | 数 | 55 |
S |
几分之一秒 | 分数 | 978 |
A |
毫日数 | 数 | 1234 |
n |
纳秒 | 数 | 987654321 |
N |
纳米日光 | 数 | 1234000000 |
V |
时区 ID | 区域 ID | 美洲/Los_Angeles;Z;-08:30 |
z |
时区名称 | 区域名称 | 太平洋标准时间;太平洋标准时间(PST) |
O |
局部区域偏移 | 偏移-O | 格林威治标准时间+8;格林威治标准时间+08:00;UTC-08:00 巴布亚新几内亚; |
X |
区域偏移“Z”表示零 | 偏移量-X | Z;-08;-0830;-08:30;-083015;-08:30:15; |
x |
区域偏移 | 偏移-x | +0000;-08;-0830;-08:30;-083015;-08:30:15; |
Z |
区域偏移 | 偏移-Z | +0000;-0800;-08:00; |
请注意,它有几个预定义的格式化程序,用于更流行的模式。因此,您可以使用 .这是可能的,因为它们与线程安全相反。因此,如有必要,您也可以定义自己的定义。DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
DateTimeFormatter.RFC_1123_DATE_TIME
SimpleDateFormat
对于特定的输入字符串格式,您不需要使用显式:标准 ISO 8601 日期(如 2016-09-26T17:44:57Z)可以直接使用 LocalDateTime#parse(text)
解析,因为它已经使用了 ISO_LOCAL_DATE_TIME
格式化程序。同样,LocalDate#parse(text) 解析不带时间组件的 ISO 日期(参见 ISO_LOCAL_DATE
),而 ZonedDateTime#parse(text)
解析添加了偏移量和时区的 ISO 日期(
参见 ISO_ZONED_DATE_TIME
)。DateTimeFormatter
Java 8 之前的版本
如果您还没有使用 Java 8,或者被迫使用 ,请使用与输入字符串匹配的格式模式使用 SimpleDateFormat
格式化日期。java.util.Date
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
请注意显式参数的重要性。如果省略它,则它将使用默认区域设置,该区域设置不一定是输入字符串的月份名称中使用的英语。如果语言环境与输入字符串不匹配,那么即使格式模式似乎有效,您也会感到困惑。Locale
java.text.ParseException
下面是从 javadoc 中提取的相关性,列出了所有可用的格式模式:
信 | 日期或时间组件 | 介绍 | 例子 |
---|---|---|---|
G |
时代标志 | 发短信 | 广告 |
y |
年 | 年 | 1996;96 |
Y |
周年 | 年 | 2009;09 |
M /L |
月度 | 月 | 七月;七月;07 |
w |
一年中的周 | 数 | 27 |
W |
周复一月 | 数 | 2 |
D |
一年中的一天 | 数 | 189 |
d |
月中的某一天 | 数 | 10 |
F |
月中的某一天 | 数 | 2 |
E |
一周中的某一天 | 发短信 | 星期二;星期二 |
u |
周的天数 | 数 | 1 |
a |
Am/pm 标记 | 发短信 | 下午 |
H |
一天中的小时 (0-23) | 数 | 0 |
k |
一天中的小时 (1-24) | 数 | 24 |
K |
上午/下午 (0-11) 小时 | 数 | 0 |
h |
上午/下午 (1-12) 小时 | 数 | 12 |
m |
分钟(以小时为单位) | 数 | 30 |
s |
以秒为单位 | 数 | 55 |
S |
毫秒 | 数 | 978 |
z |
时区 | 一般时区 | 太平洋标准时间;太平洋标准时间;格林威治标准时间-08:00 |
Z |
时区 | RFC 822 时区 | -0800 |
X |
时区 | ISO 8601 时区 | -08;-0800;-08:00 |
请注意,这些模式区分大小写,并且四个或更多字符的基于文本的模式表示完整形式;否则,如果可用,则使用简短或缩写形式。例如 或更多是不必要的。MMMMM
以下是迄今为止用于解析给定字符串的一些有效模式示例:SimpleDateFormat
输入字符串 | 模式 |
---|---|
2001.07.04 公元 12:08:56 PDT | yyyy.MM.dd G 'at' HH:mm:ss z |
周三, 7月 4, '01 | EEE, MMM d, ''yy |
下午12:08 | h:mm a |
太平洋夏令时间中午 12 点 | hh 'o''clock' a, zzzz |
下午 0:08,太平洋夏令时 | K:mm a, z |
02001.七月.04 公元 12:08 下午 | yyyyy.MMMM.dd GGG hh:mm aaa |
2001 年 7 月 4 日星期三 12:08:56 -0700 | EEE, d MMM yyyy HH:mm:ss Z |
010704120856-0700 | yyMMddHHmmssZ |
2001-07-04T12:08:56.235-0700 | yyyy-MM-dd'T'HH:mm:ss.SSSZ |
2001-07-04T12:08:56.235-07:00 | yyyy-MM-dd'T'HH:mm:ss.SSSXXX |
2001年W27-3 | YYYY-'W'ww-u |
需要注意的是,它不是线程安全的。换句话说,你永远不应该将它声明并分配为静态或实例变量,然后从不同的方法/线程中重用它。您应该始终在方法本地范围内创建全新的它。SimpleDateFormat
评论
O
UTC+08:00
GMT+08:00
20-JUN-16 12.00.00.000000000 AM
啊,是的,Java Date 讨论,再次。为了处理日期操作,我们使用 Date、Calendar、GregorianCalendar 和 SimpleDateFormat。 例如,使用 1 月日期作为输入:
Calendar mydate = new GregorianCalendar();
String mystring = "January 2, 2010";
Date thedate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(mystring);
mydate.setTime(thedate);
//breakdown
System.out.println("mydate -> "+mydate);
System.out.println("year -> "+mydate.get(Calendar.YEAR));
System.out.println("month -> "+mydate.get(Calendar.MONTH));
System.out.println("dom -> "+mydate.get(Calendar.DAY_OF_MONTH));
System.out.println("dow -> "+mydate.get(Calendar.DAY_OF_WEEK));
System.out.println("hour -> "+mydate.get(Calendar.HOUR));
System.out.println("minute -> "+mydate.get(Calendar.MINUTE));
System.out.println("second -> "+mydate.get(Calendar.SECOND));
System.out.println("milli -> "+mydate.get(Calendar.MILLISECOND));
System.out.println("ampm -> "+mydate.get(Calendar.AM_PM));
System.out.println("hod -> "+mydate.get(Calendar.HOUR_OF_DAY));
然后你可以用类似的东西来操纵它:
Calendar now = Calendar.getInstance();
mydate.set(Calendar.YEAR,2009);
mydate.set(Calendar.MONTH,Calendar.FEBRUARY);
mydate.set(Calendar.DAY_OF_MONTH,25);
mydate.set(Calendar.HOUR_OF_DAY,now.get(Calendar.HOUR_OF_DAY));
mydate.set(Calendar.MINUTE,now.get(Calendar.MINUTE));
mydate.set(Calendar.SECOND,now.get(Calendar.SECOND));
// or with one statement
//mydate.set(2009, Calendar.FEBRUARY, 25, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
System.out.println("mydate -> "+mydate);
System.out.println("year -> "+mydate.get(Calendar.YEAR));
System.out.println("month -> "+mydate.get(Calendar.MONTH));
System.out.println("dom -> "+mydate.get(Calendar.DAY_OF_MONTH));
System.out.println("dow -> "+mydate.get(Calendar.DAY_OF_WEEK));
System.out.println("hour -> "+mydate.get(Calendar.HOUR));
System.out.println("minute -> "+mydate.get(Calendar.MINUTE));
System.out.println("second -> "+mydate.get(Calendar.SECOND));
System.out.println("milli -> "+mydate.get(Calendar.MILLISECOND));
System.out.println("ampm -> "+mydate.get(Calendar.AM_PM));
System.out.println("hod -> "+mydate.get(Calendar.HOUR_OF_DAY));
评论
在处理 SimpleDateFormat 类时,请务必记住 Date 不是线程安全的,并且不能与多个线程共享单个 Date 对象。
此外,“m”和“M”之间也有很大的区别,其中小写字母用于分钟,大写字母用于月。“d”和“D”相同。这可能会导致经常被忽视的细微错误。有关更多详细信息,请参阅 Javadoc 或 Java 中将字符串转换为日期的指南。
评论
此外,SimpleDateFormat 不适用于某些客户端技术,如 GWT。
使用 Calendar.getInstance() 是个好主意,您的要求是比较两个日期;去长期约会。
String str_date = "11-June-07";
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = formatter.parse(str_date);
评论
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = dateFormat.parse("2013-12-4");
System.out.println(date.toString()); // Wed Dec 04 00:00:00 CST 2013
String output = dateFormat.format(date);
System.out.println(output); // 2013-12-04
}
catch (ParseException e) {
e.printStackTrace();
}
它对我来说很好用。
评论
虽然有些答案在技术上是正确的,但它们是不可取的。
- java.util.Date 和 Calendar 类是出了名的麻烦。由于设计和实现中的缺陷,请避免它们。幸运的是,我们还可以选择另外两个优秀的日期时间库:
- Joda-Time
这个流行的开源免费库可以在多个版本的 Java 中使用。可以在 StackOverflow 上找到许多使用它的示例。阅读其中一些将帮助您快速上手。 - java.time.* 包
这组新类的灵感来自 Joda-Time,并由 JSR 310 定义。这些类内置于 Java 8 中。一个项目正在进行中,将这些类向后移植到 Java 7,但 Oracle 不支持这种向后移植。
- Joda-Time
- 正如克里斯托弗·约翰逊(Kristopher Johnson)在对这个问题的评论中正确指出的那样,其他答案忽略了以下重要问题:
- Time of Day
既有日期部分,也有时间部分) - 时区 一天的开始取决于时区
。如果未能指定时区,那么将应用 JVM 的缺省时区。这意味着,在其他计算机上运行时或修改时区设置时,代码的行为可能会发生变化。可能不是你想要的。 - 区域设置
语言设置的语言指定如何解释在分析过程中遇到的单词(月份和日期的名称)。(BalusC的答案正确地处理了这一点。此外,在生成日期时间的字符串表示形式时,Locale 会影响某些格式化程序的输出。
- Time of Day
Joda-时间
以下是关于Joda-Time的一些说明。
时区
在 Joda-Time 中,DateTime 对象真正知道自己分配的时区。这与 java.util.Date 类形成鲜明对比,该类似乎有时区,但没有。
请注意,在下面的示例代码中,我们如何将时区对象传递给解析字符串的格式化程序。该时区用于将该日期时间解释为发生在该时区。因此,您需要考虑并确定该字符串输入所代表的时区。
由于输入字符串中没有时间部分,因此 Joda-Time 将指定时区一天中的第一个时刻指定为一天中的时间。通常,由于夏令时 (DST) 或其他异常情况,这意味着但并非总是如此。顺便说一句,您可以通过调用 对任何 DateTime 实例执行相同的操作。00:00:00
withTimeAtStartOfDay
格式化程序模式
格式化程序模式中使用的字符在 Joda-Time 中与 java.util.Date/Calendar 中的字符相似,但并不完全相同。仔细阅读文档。
不可变性
我们通常在 Joda-Time 中使用不可变的类。我们调用的方法不是修改现有的 Date-Time 对象,而是基于另一个对象创建新的新实例,并复制大多数方面,除非需要更改。一个例子是下面最后一行中的调用。不可变性有助于使 Joda-Time 非常线程安全,还可以使一些工作更加清晰。withZone
转换
您将需要 java.util.Date 对象来与其他不知道 Joda-Time 对象的类/框架一起使用。幸运的是,来回移动非常容易。
从 java.util.Date 对象(此处命名为 )到 Joda-Time DateTime...date
org.joda.time.DateTime dateTime = new DateTime( date, timeZone );
从 Joda-Time 转到另一个方向到 java.util.Date 对象...
java.util.Date date = dateTime.toDate();
示例代码
String input = "January 2, 2010";
java.util.Locale locale = java.util.Locale.US;
DateTimeZone timeZone = DateTimeZone.forID( "Pacific/Honolulu" ); // Arbitrarily chosen for example.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MMMM d, yyyy" ).withZone( timeZone ).withLocale( locale );
DateTime dateTime = formatter.parseDateTime( input );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime in UTC/GMT: " + dateTime.withZone( DateTimeZone.UTC ) );
运行时...
dateTime: 2010-01-02T00:00:00.000-10:00
dateTime in UTC/GMT: 2010-01-02T10:00:00.000Z
在 Java 8 中,我们得到了一个新的日期/时间 API (JSR 310)。
以下方法可用于解析 Java 8 中的日期,而无需依赖 Joda-Time:
String str = "January 2nd, 2010";
// if we 2nd even we have changed in pattern also it is not working please workout with 2nd
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM Q, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(str, formatter);
// access date fields
int year = date.getYear(); // 2010
int day = date.getDayOfMonth(); // 2
Month month = date.getMonth(); // JANUARY
int monthAsInt = month.getValue(); // 1
LocalDate 是用于表示日期(不带时间)的标准 Java 8 类。如果要分析包含日期和时间信息的值,则应使用 LocalDateTime。对于具有时区的值,请使用 ZonedDateTime。两者都提供了类似于以下内容的方法:parse()
LocalDate
LocalDateTime dateWithTime = LocalDateTime.parse(strWithDateAndTime, dateTimeFormatter);
ZonedDateTime zoned = ZonedDateTime.parse(strWithTimeZone, zoneFormatter);
来自 DateTimeFormatter Javadoc 的列表格式化字符:
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters.
The following pattern letters are defined:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;
评论
我简陋的测试程序。我用它来玩弄格式化程序并查找我在日志文件中找到的长日期(但是谁把它们放在那里......
我的测试程序:
package be.test.package.time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class TimeWork {
public static void main(String[] args) {
TimeZone timezone = TimeZone.getTimeZone("UTC");
List<Long> longs = new ArrayList<>();
List<String> strings = new ArrayList<>();
//Formatting a date needs a timezone - otherwise the date get formatted to your system time zone.
//Use 24h format HH. In 12h format hh can be in range 0-11, which makes 12 overflow to 0.
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
formatter.setTimeZone(timezone);
Date now = new Date();
//Test dates
strings.add(formatter.format(now));
strings.add("01-01-1970 00:00:00.000");
strings.add("01-01-1970 00:00:01.000");
strings.add("01-01-1970 00:01:00.000");
strings.add("01-01-1970 01:00:00.000");
strings.add("01-01-1970 10:00:00.000");
strings.add("01-01-1970 12:00:00.000");
strings.add("01-01-1970 24:00:00.000");
strings.add("02-01-1970 00:00:00.000");
strings.add("01-01-1971 00:00:00.000");
strings.add("01-01-2014 00:00:00.000");
strings.add("31-12-1969 23:59:59.000");
strings.add("31-12-1969 23:59:00.000");
strings.add("31-12-1969 23:00:00.000");
//Test data
longs.add(now.getTime());
longs.add(-1L);
longs.add(0L); //Long date presentation at - midnight 1/1/1970 UTC - The timezone is important!
longs.add(1L);
longs.add(1000L);
longs.add(60000L);
longs.add(3600000L);
longs.add(36000000L);
longs.add(43200000L);
longs.add(86400000L);
longs.add(31536000000L);
longs.add(1388534400000L);
longs.add(7260000L);
longs.add(1417706084037L);
longs.add(-7260000L);
System.out.println("===== String to long =====");
//Show the long value of the date
for (String string: strings) {
try {
Date date = formatter.parse(string);
System.out.println("Formated date : " + string + " = Long = " + date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
System.out.println("===== Long to String =====");
//Show the date behind the long
for (Long lo : longs) {
Date date = new Date(lo);
String string = formatter.format(date);
System.out.println("Formated date : " + string + " = Long = " + lo);
}
}
}
测试结果:
===== String to long =====
Formated date : 05-12-2014 10:17:34.873 = Long = 1417774654873
Formated date : 01-01-1970 00:00:00.000 = Long = 0
Formated date : 01-01-1970 00:00:01.000 = Long = 1000
Formated date : 01-01-1970 00:01:00.000 = Long = 60000
Formated date : 01-01-1970 01:00:00.000 = Long = 3600000
Formated date : 01-01-1970 10:00:00.000 = Long = 36000000
Formated date : 01-01-1970 12:00:00.000 = Long = 43200000
Formated date : 01-01-1970 24:00:00.000 = Long = 86400000
Formated date : 02-01-1970 00:00:00.000 = Long = 86400000
Formated date : 01-01-1971 00:00:00.000 = Long = 31536000000
Formated date : 01-01-2014 00:00:00.000 = Long = 1388534400000
Formated date : 31-12-1969 23:59:59.000 = Long = -1000
Formated date : 31-12-1969 23:59:00.000 = Long = -60000
Formated date : 31-12-1969 23:00:00.000 = Long = -3600000
===== Long to String =====
Formated date : 05-12-2014 10:17:34.873 = Long = 1417774654873
Formated date : 31-12-1969 23:59:59.999 = Long = -1
Formated date : 01-01-1970 00:00:00.000 = Long = 0
Formated date : 01-01-1970 00:00:00.001 = Long = 1
Formated date : 01-01-1970 00:00:01.000 = Long = 1000
Formated date : 01-01-1970 00:01:00.000 = Long = 60000
Formated date : 01-01-1970 01:00:00.000 = Long = 3600000
Formated date : 01-01-1970 10:00:00.000 = Long = 36000000
Formated date : 01-01-1970 12:00:00.000 = Long = 43200000
Formated date : 02-01-1970 00:00:00.000 = Long = 86400000
Formated date : 01-01-1971 00:00:00.000 = Long = 31536000000
Formated date : 01-01-2014 00:00:00.000 = Long = 1388534400000
Formated date : 01-01-1970 02:01:00.000 = Long = 7260000
Formated date : 04-12-2014 15:14:44.037 = Long = 1417706084037
Formated date : 31-12-1969 21:59:00.000 = Long = -7260000
我们使用了简单的两个格式化程序:
- 我们想要哪种格式的日期?
- 实际存在的日期格式是什么?
我们解析完整的日期到时间格式:
date="2016-05-06 16:40:32";
public static String setDateParsing(String date) throws ParseException {
// This is the format date we want
DateFormat mSDF = new SimpleDateFormat("hh:mm a");
// This format date is actually present
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd hh:mm");
return mSDF.format(formatter.parse(date));
}
试试这个
String date = get_pump_data.getString("bond_end_date");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date datee = (Date)format.parse(date);
可以使用 SimpleDateformat 将字符串更改为日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strDate = "2000-01-01";
Date date = sdf.parse(strDate);
评论
源链接
对于Android
Calendar.getInstance().getTime() 给出
Thu Jul 26 15:54:13 GMT+05:30 2018
用
String oldDate = "Thu Jul 26 15:54:13 GMT+05:30 2018";
DateFormat format = new SimpleDateFormat("EEE LLL dd HH:mm:ss Z yyyy");
Date updateLast = format.parse(oldDate);
字符串到日期的转换:
private Date StringtoDate(String date) throws Exception {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date sqlDate = null;
if( !date.isEmpty()) {
try {
java.util.Date normalDate = sdf1.parse(date);
sqlDate = new java.sql.Date(normalDate.getTime());
} catch (ParseException e) {
throw new Exception("Not able to Parse the date", e);
}
}
return sqlDate;
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date1 = null;
Date date2 = null;
try {
date1 = dateFormat.parse(t1);
date2 = dateFormat.parse(t2);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String StDate = formatter.format(date1);
String edDate = formatter.format(date2);
System.out.println("ST "+ StDate);
System.out.println("ED "+ edDate);
评论
SimpleDateFormat
SimpleDateFormat
DateFormat
Date
LocalDateTime
DateTimeFormatter
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
return sdf.format(date);
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
return sdf.parse(dateStr);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat newSdf = new SimpleDateFormat("dd-MM-yyyy");
Date temp = sdf.parse(dateStr);
return newSdf.format(temp);
源链接。
我致力于将 String 解析为 LocalDateTime。我把它留在这里作为例子
LocalDateTime d = LocalDateTime.parse("20180805 101010", DateTimeFormatter.ofPattern("yyyyMMdd HHmmss"));
下一个:如何在 C 中正确比较字符串?
评论
Date
java.util.Date、java.util.Calendar
,现在是遗留的,被 java.time 类取代。请参见 Oracle 提供的教程。
java.text.SimpleTextFormat