tl;dr
LocalDateTime.of(
LocalDate.parse(
"31/01/2012" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
) ,
LocalTime.parse( "20:00" )
).atZone( ZoneId.of( "Africa/Tunis" ) )
2012-01-31T20:00+01:00[Africa/Tunis]
See this code run live at IdeOne.com.
Using java.time
You are using troublesome old date-time classes, now supplanted by the java.time classes.
You need not concatenate the strings. Parse each, one as a LocalDate
and one as a LocalTime
.
DateTimeFormatter fDate = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.format( "31/01/2012" , fDate ) ;
LocalTime lt = LocalTime.parse( "20:00" ) ; // Built-in formatter handles standard ISO 8601 formats.
You can combine those two parts into LocalDateTime
. A LocalDateTime
has no concept of time zone nor offset-from-UTC.
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;
We have no indication of offset-from-UTC nor time zone. So we do not have a specific moment, on an idea about possible moments. For example, 8 PM comes much earlier in New Zealand (Pacific/Auckland
) than it does in India (Asia/Kolkata
), and comes hours later in France (Europe/Paris
).
To determine a specific moment, we must put that LocalDateTime
in the context of a time zone. Perhaps you intended that moment for UTC.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
Or perhaps you meant that for Québec time.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.