Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to parse correctly a Date in Kotlin?

Posted on November 14, 2022

Solution 1 :

tl;dr

myPreparedStatement.setObject( … , LocalDate.parse( "2020-07-07" ) ) 

Date class — not for dates

One of many flaws in the java.util.Date class is its name. It does not represent a date. That class represents a moment, a date with time-of-day as seen in UTC.

Adding to the confusion is that its toString method dynamically applies the JVM’s current default time zone.

Never use Date

Both the java.util.Date class and java.sql.Date class are terrible, flawed in design, written by people who did not understand date-time handling. Avoid them, along with SimpleDateFormat etc. Sun, Oracle, and the JCP community gave up on those classes years ago, and so should you.

java.time

Those legacy classes were supplanted years ago by the modern java.time classes.

Your input string is in standard ISO 8601 format. The java.time classes use those formats by default when parsing/generating strings.

LocalDate ld = LocalDate.parse( "2020-07-07" ) ;

Database

You can exchange java.time object with your database as of JDBC 4.2 and later. Be sure your JDBC driver is up-to-date.

You should be writing a LocalDate object to a column of a type akin to the SQL-standard type DATE.

myPreparedStatement.setObject( … , ld ) ;

Retrieval.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

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.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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. Hibernate 5 & JPA 2.2 support java.time.

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 brought 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 (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

Table of which java.time library to use with which version of Java or Android

Solution 2 :

Object of class Date can be printed in many formats.

val date: String = "2020-07-07"
val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.US) 
val dateDate: Date = formatter.parse(date) // You got Date object of 2020 jul 7

println(formatter.format(dateDate)) //it prints "2020-07-07"
println(dateDate)                   // without formatter it print default format 
                                    // "Tue Jul 07 00:00:00 GMT-03:00 2020"

Solution 3 :

I believe you already have the right code.
But the problem seems to be that you should use the correct string.
You present 2020-08-06 but then change it to 2020-07-07.

The complete code is

import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

@Throws(ParseException::class)
fun main() {
    val rawDate: String = "2020-08-06"
    val format: DateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
    val date: Date = format.parse(rawDate)
    println(date)
}

I get Thu Aug 06 00:00:00 CEST 2020 with CEST because I’m located in central Europe.

READ  [ANSWERED] android - How do I set CountDownTimer to be 10 minutes?
Powered by Inline Related Posts

Also, this post Java string to date conversion covers a lot of what you seek.

Solution 4 :

val dateString = "2020-07-05"
dateString.format(DateTime())
//Log.d("DateTest", dateString.format(DateTime()))

Try this. It should work. This converts a string to a DateTime() format.

Solution 5 :

Have you tried using Locale.Default?

Solution 6 :

you use Android so your DB is most likely SQLite (also when you use Room). That means that you cannot store data as Date, see here
https://www.sqlitetutorial.net/sqlite-date/

SQLite does not support built-in date and/or time storage class. Instead, it leverages some built-in date and time functions to use other storage classes such as TEXT, REAL, or INTEGER for storing the date and time values.

So basically you can store it e.g. as Int and then when you retrieve it you can convert it to Date

Solution 7 :

val date = "2020-07-07"

val formatter = SimpleDateFormat("yyyy-MM-dd") // change in this line 

formatter.parse(date)

This will work surely

Problem :

I am having the following error. I have a String = “2020-07-07” and I want it as Date so I can save it in my DB. So I did this:

val date = "2020-07-07"

val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.US) 

formatter.parse(date)

But I am getting this:

Tue Jul 07 00:00:00 GMT-03:00 2020

What I am doing wrong? Please Help!

I have tried with formatter.format, but that returns a String and I want it to be a Date

The type of the variable after parsing MUST be Date, not LocalDate.

Comments

Comment posted by Ben P.

It sounds like you have a misunderstanding of what a

Comment posted by Alejoo Gonçalves

@BenP. What I meant was that I just want to convert the String to a Date, without changing the content

Comment posted by Ben P.

Which is exactly what you have done

READ  [ANSWERED] android - why is 'location cannot be determined' coming up on devices?
Powered by Inline Related Posts

Comment posted by java.util.Date

FYI, the terribly flawed date-time classes such as

Comment posted by Basil Bourque

These flawed classes were supplanted years ago by the

Comment posted by Alejoo Gonçalves

But the problem is that I dont want to get something like Thu Aug 06 00:00:00 CEST 2020. I just want the same content as the String (2020-07-07) but as Date. You know what I mean?

Comment posted by Little Helper

To be sure: You have a variable of type

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress