Convert the string of date and time (2018-04-13T20:00:00.0400) into date string (April 13, 2018) in Kotlin
tl;dr
Use modern java.time classes, never the terrible legacy classes.
Here is Java syntax (I have not yet learned Kotlin).
LocalDateTime // Represent a date with time-of-day but lacking offset-from-UTC or time zone. As such, this does *not* represent a moment, is *not* a point on the timeline.
.parse( "2018-04-13T20:00:00.0400" ) // Parse an input string in standard ISO 8601 format. Returns a `LocalDateTime` object.
.toLocalDate() // Extract the date-only portion without the time-of-day. Still no time zone or offset-from-UTC. Returns a `LocalDate` object.
.format( // Generate text representing the value of that `LocalDate` object.
DateTimeFormatter // Define a pattern to use in generating text.
.ofLocalizedDate( FormatStyle.LONG ) // Automatically localize, specifying how long/abbreviated…
.withLocale( Locale.US ) // … and what human language and cultural norms to use in localizing.
) // Return a `String`.
April 13, 2018
For earlier Android, see bullets at bottom below.
Use java.time
convert the DateTime string like "2018-04-13T20:00:00.0400" into "April 13, 2018".
You are using terrible old date-time classes that were supplanted years ago by the java.time classes.
First, parse your input string. But what is that .0400
on the end? Perhaps a fractional second? But conventionally the milliseconds, microseconds, or nanoseconds are displayed in groups of 3 digits. So your four digits here is odd. Perhaps an offset-from-UTC? Four digits is right for that, for hours and minutes with leading zero. But there is no plus or minus sign to indicate ahead-of or behind UTC. So I'll go with the fractional second.
Your input lacks any indicator of offset-from-UTC or time zone. So parse as a LocalDateTime
. Your string is in standard ISO 8601 format. The standard formats are used by default in the java.time classes.
String input = "2018-04-13T20:00:00.0400";
LocalDateTime ldt = LocalDateTime.parse( input );
ldt.toString(): 2018-04-13T20:00:00.040
Extract the date portion.
LocalDate ld = ldt.toLocalDate() :
ld.toString(): 2018-04-13
Generate text representing that date's value. Let java.time automatically localize. To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.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.
Code:
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG ).withLocale( Locale.US );
String output = ld.format( f );
April 13, 2018
Beware: A LocalDateTime
object is not a moment, is not a point on the timeline. Lacking any offset-from-UTC or time zone means it represent a range of potential moments, along a range of about 26-27 hours (the current range of time zones around the globe). If the input string were implicitly intended to represent a moment in the wall-clock time of some region, you should apply a ZoneId
to get a ZonedDateTime
object before extracting a LocalDate
. This has been shown many times before on Stack Overflow.
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?
- 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 adds 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 bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
A fixed version of the above, since your Date input doesn't have a Z in it. And their output format doesn't match yours. I would suggest reading the docs https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html that way you can modify the responses as needed.
fun convertISOTimeToDate(isoTime: String): String? {
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
var convertedDate: Date? = null
var formattedDate: String? = null
try {
convertedDate = sdf.parse(isoTime)
formattedDate = SimpleDateFormat("MMMMM dd,yyyy").format(convertedDate)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedDate
}