tl;dr
Use modern java.time classes.
Here is a brief but nonsensical example of going from current moment in a time zone to text in your desired format without zone/offset, and then back again to a moment in a time zone.
LocalDateTime.parse( // Represent a date and time-of-day without a time zone or offset-from-UTC. **NOT* a moment, but represents potential moments along a range of about 26-27 hours.
ZonedDateTime.now( ZoneId.of( "Asia/Kuwait" ) ) // Represents a moment, a point on the timeline, a date and time-of-day with an assigned time zone.
.format( DateTimeFormatter.ofPattern( "dd-MMM-yyyy HH:mm:ss" , Locale.US ) ) // Generate a `String` with text representing the value of our `ZonedDateTime` object.
,
DateTimeFormatter.ofPattern( "dd-MMM-yyyy HH:mm:ss" , Locale.US ) // Specify `Locale` to determine the human language and cultural norms used in localizing such as parsing the name of the month.
) // Returns a `LocalDateTime` object.
.atZone( ZoneId.of( "Asia/Kuwait" ) ) // Assign a time zone to the `LocalDateTime` to determine a moment, to generate a `ZonedDateTime` object.
java.time
You are using terrible old date-time classes that were supplanted years ago by the java.time classes.
Also, I suspect you meant HH
for 24-hour clock rather than lowercase hh
for 12-hour clock.
First, get the current moment.
ZoneId z = ZoneId.of( "Asia/Kuwait" ); // FYI, `Asia/Kuwait` is an alias of `Asia/Riyadh`.
ZonedDateTime zdt = ZonedDateTime.now( z );
zdt.toString(): 2018-09-27T22:41:45.314343+03:00[Asia/Kuwait]
Next, generate text in your desired format to represent the value of our ZonedDateTime
. Important: Notice that your string format is incomplete, lacking the fraction-of-second and lacking any indicator of time zone or offset-from-UTC.
Define a formatting pattern. Specify a Locale
to determine:
- The human language for translation of name of day, name of month, and such.
- The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
Get a DateTimeFormatter
object.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-yyyy HH:mm:ss" , Locale.US );
Generate text.
String output = zdt.format( f );
27-Sep-2018 22:41:45
Going back the other direction is trickier because your desired format for this string lacks any indicator of time zone or offset-from-UTC. Such a string does not represent a moment. Taken at face value, it could be any of many potential moments along a range of 26-27 hours (the range of time zones around the globe). I know, and you know, that the string is intended to be a moment in the time zone Asia/Kuwait
with an offset-from-UTC of three hours ahead. But the string does not literally say that.
So we must first parse the string as a LocalDateTime
, having no zone/offset.
LocalDateTime ldt = LocalDateTime.parse( output , f );
ldt.toString(): 2018-09-27T22:41:45
Then we inject the intended zone to determine a moment.
ZonedDateTime zdt2 = ldt.atZone( z );
zdt2.toString(): 2018-09-27T22:41:45+03:00[Asia/Kuwait]
To close the circle, let’s compare our second ZonedDateTime
object with the first. To do fairly, we must lop off the fraction-of-second omitted from your desired text format.
boolean sameZdt = zdt.truncatedTo( ChronoUnit.SECONDS ).isEqual( zdt2 );
true
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.
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.
Where to obtain the java.time classes?