简体   繁体   中英

How to parse correctly a Date in Kotlin?

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.

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.time 库与哪个版本的 Java 或 Android 一起使用的表

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"

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.

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

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.

Have you tried using Locale.Default?

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 eg as Int and then when you retrieve it you can convert it to Date

val date = "2020-07-07"

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

formatter.parse(date)

This will work surely

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM