Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert the string of date and time (2018-04-13T20:00:00.0400) into date string (April 13, 2018) in Kotlin

I want to convert the DateTime string like "2018-04-13T20:00:00.0400" into "April 13, 2018". I have used the code

val sdf = SimpleDateFormat("yyyy/mm/dd", Locale.getDefault())
val startDate = sdf.parse(data.start_time)

but with this code, my app crashes. Any help regarding this

Log is showing this

2018-10-11 16:44:56.948 20482-21021/? E/NowController: Failed to access data from EntryProvider. ExecutionException.
java.util.concurrent.ExecutionException: com.google.android.apps.gsa.sidekick.main.h.n: Could not complete scheduled request to refresh entries. ClientErrorCode: 3
    at com.google.common.util.concurrent.d.eA(SourceFile:85)
    at com.google.common.util.concurrent.d.get(SourceFile:23)
    at com.google.common.util.concurrent.l.get(SourceFile:2)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbB(SourceFile:49)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbA(SourceFile:181)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.bh.run(Unknown Source:2)
    at com.google.android.apps.gsa.shared.util.concurrent.at.run(SourceFile:4)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)
    at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
    at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
    at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)
 Caused by: com.google.android.apps.gsa.sidekick.main.h.n: Could not complete scheduled request to refresh entries. ClientErrorCode: 3
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.ar.az(Unknown Source:4)
    at com.google.common.util.concurrent.q.ap(SourceFile:7)
    at com.google.common.util.concurrent.p.run(SourceFile:32)
    at com.google.common.util.concurrent.bt.execute(SourceFile:3)
    at com.google.common.util.concurrent.d.b(SourceFile:275)
    at com.google.common.util.concurrent.d.addListener(SourceFile:135)
    at com.google.common.util.concurrent.p.b(SourceFile:3)
    at com.google.android.apps.gsa.shared.util.concurrent.h.a(SourceFile:16)
    at com.google.android.apps.gsa.shared.util.concurrent.h.a(SourceFile:13)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbB(SourceFile:47)
like image 464
Ali Yar Khan Avatar asked Oct 11 '18 11:10

Ali Yar Khan


People also ask

How do you convert string to time?

You can use the following code for changing the String value into the time equivalent: String str = "08:03:10 pm"; DateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); Date date = (Date)formatter. parse(str);

How do I change the Date format in Kotlin?

The default way of formatting Date using default format in Kotlin is invoking the toString() method. This looks readable as the output format is yyyy-MM-dd, but again, we may need to format the date to custom formats depending on our use-cases.


3 Answers

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.

like image 195
Basil Bourque Avatar answered Oct 10 '22 18:10

Basil Bourque


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
}
like image 4
EJG Avatar answered Oct 10 '22 19:10

EJG


Kotlin Syntax LocaleDateTime.parse (Examples) =>

val date = LocalDateTime
    .parse(q.ADDDATE)
    .toLocalDate()
    .format(
        DateTimeFormatter
            .ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.ENGLISH)
    )

val dateView = date
like image 1
Zafer Aydemir Avatar answered Oct 10 '22 17:10

Zafer Aydemir