问题
I have to parse the following date
Fri Sep 30 18:31:00 GMT+04:00 2016
and it is not working with the following pattern:
new SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY", Locale.ENGLISH);
I get the following date as output: Fri Jan 01 18:31:00 GMT+04:00 2016.
Could you please tell me what I am doing wrong?
回答1:
It should be lower case "y":
EEE MMM dd HH:mm:ss z yyyy
Upper case "Y" means weekBasedYear:
a date can be created from a week-based-year, week-of-year and day-of-week
I guess mixing the week-based and absolute/era patterns just does not work well for parsing.
回答2:
tl;dr
OffsetDateTime
.parse
(
"Fri Sep 30 18:31:00 GMT+04:00 2016" ,
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss O uuuu" , Locale.US )
)
.toString()
2016-09-30T18:31+04:00
Avoid legacy classes
The other Answers are now outmoded. The terrible Date
, Calendar
, and SimpleDateFormat
classes are now legacy, supplanted years ago by the modern java.time classes defined in JSR 310.
java.time
Your input string represents a date with time-of-day in the context of an offset-from-UTC. An offset is a number of hours ahead or behind of the baseline of UTC. For this kind of information, use OffsetDateTime
class.
Note that an offset-from-UTC is not a time zone. A time zone is a history of the past, present, and future changes to the offset used by the people of a particular region.
The DateTimeFormatter class replaces SimpleDateFormat
. The formatting codes are similar, but not exactly the same. So carefully study the Javadoc.
Notice that we pass a Locale
. This specifies the human language and cultural norms to use in parsing the input, such as the name of the month, name of the day-of-week, the punctuation, the capitalization, and so on.
package work.basil.example.datetime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Parsing
{
public static void main ( String[] args )
{
Parsing app = new Parsing();
app.demo();
}
private void demo ( )
{
String input = "Fri Sep 30 18:31:00 GMT+04:00 2016";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss O uuuu" , Locale.US );
OffsetDateTime odt = OffsetDateTime.parse( input , f );
System.out.println( "odt = " + odt );
}
}
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
回答3:
Below code is working fine
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class ParseDate {
public static void main(String[] args) {
try {
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Fri Sep 30 18:31:00 GMT+04:00 2016");
System.out.println("date: " + date.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
来源:https://stackoverflow.com/questions/39785092/java-date-format-gmt-0400