I have a String as
strikedate="2011-11-19T00:00:00.000-05:00"
I need to put this into a java.util.Date Could anybody please tell me how to convert this String to Date
Sure - use SimpleDateFormat
.
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date parsed = sdf.parse(text);
EDIT: Eek - it's not quite that simple, due to your time zone format. The above will work without the colon. You may need to use Joda Time to parse the version with the colon. Checking that now...
EDIT: Yes, with Joda Time you can use:
DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
DateTime parsed = formatter.parseDateTime(text);
// If you really need it
Date date = parsed.toDate();
Joda Time is also a much nicer date/time API to start with - I'd strongly encourage you to use it if you can.
Use a SimpleDateFormat (namely, its inherited parse()
method).
The format string should be "YYYY-MM-dd'T'HH:mm:ss.SSSZ" - though that expectes the zimezone to come without a colon, so you may have to preprocess the string to remove the colon.
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice at the Home Page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
java.time
, the modern Date-Time API:The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter
object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.
Demo:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-19T00:00:00.000-05:00");
System.out.println(odt);
}
}
Output:
2011-11-19T00:00-05:00
For any reason, if you need to convert this object of OffsetDateTime
to an object of java.util.Date
, you can do so as follows:
Date date = Date.from(odt.toInstant());
Check this answer and this answer to learn how to use the modern Date-Time API with JDBC.
Learn more about the modern Date-Time API from Trail: Date Time.
For the sake of completeness, given below is the solution using SimpleDateFormat
:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-19T00:00:00.000-05:00");
// Display date in UTC/GMT
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(date));
// Display date in the timezone with an offset of -05:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-05:00"));
System.out.println(sdf.format(date));
}
}
Output:
2011-11-19T05:00:00.000Z
2011-11-19T00:00:00.000-05:00
Note that a java.util.Date
object does not hold any format and timezone information, it applies the format, EEE MMM dd HH:mm:ss z yyyy
and the JVM's timezone to return the value of Date#toString
derived from this milliseconds value. To get the String
representation of the java.util.Date
object in a different format and timezone, you need to use SimpleDateFormat
with the desired format and the applicable timezone.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Try something like:
String format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
offset = "GMT-04:00";
String date = "2010-10-12T20:01:18.000Z";
SimpleDateFormat sdf = new SimpleDateFormat(format);
str = sdf.parse(date);