Skip to main content
added 2 characters in body
Source Link

TL;DRJava 12

You can useSince Java 12, Instant#parse since Java 12can parse a date-time string containing time-zone offset.

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

TL;DR

You can use Instant#parse since Java 12.

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

Java 12

Since Java 12, Instant#parse can parse a date-time string containing time-zone offset.

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

added 150 characters in body
Source Link

TL;DR

You can use Instant#parse since Java 12.

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

TL;DR

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

TL;DR

You can use Instant#parse since Java 12.

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

added 76 characters in body
Source Link
Basil Bourque
  • 346.7k
  • 130
  • 950
  • 1.3k

TL;DR

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

TL;DR

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

Learn more about the modern Date-Time API from Trail: Date Time.

TL;DR

Date dt = Date.from(Instant.parse(your-date-time-string)); 

Parse your ISO 8601 formatted date-time strings using Instant#parse and convert the result into java.util.Date using Date#from. See this code run at Ideone.com.

Demo:

import java.time.Instant; import java.util.Date; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of( "2010-01-01T12:00:00+01:00", "2010-01-01T12:00:00-01:00", "2010-01-01T12:00:00Z" ) .map(Instant::parse) .map(Date::from) .forEach(System.out::println); } } 

See this code run at Ideone.com.

Learn more about the modern Date-Time API from Trail: Date Time.

Source Link
Loading