Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date String to desired format in Kotlin

I want to convert date into desired format. I know first i need to convert into date then need to format that date. My question is any easiest way to do in simple form. Is there any impact on performance? Because i have all these dates in list.

for example

fun main(vararg args: String) {
    val dateString = "2021-05-12T12:12:12.121Z"
    val convertedDate = convertDate(dateString)
    print(
            convertedDate?.let {
                formatDate(it)
            }
    )
}

fun formatDate(date: Date): String {
    return SimpleDateFormat("MMM dd, YYYY").format(date)
}

fun convertDate(dateString: String): Date? {
    return SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(dateString)
}

Output

May 12, 2021
like image 303
Vivek Modi Avatar asked Oct 21 '25 05:10

Vivek Modi


1 Answers

The standard Date-Time classes do not have any attribute to hold the formatting information. Even if some library or custom class promises to do so, it is breaking the Single Responsibility Principle. A Date-Time object is supposed to store the information about Date, Time, Timezone etc., not about the formatting. The only way to represent a Date-Time object in the desired format is by formatting it into a String using a Date-Time parsing/formatting type:

  • For the modern Date-Time API: java.time.format.DateTimeFormatter
  • For the legacy Date-Time API: java.text.SimpleDateFormat

Note that the java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*, released in March 2014 as part of Java SE 8 standard library.

Apart from that, there are many things wrong with your code:

  1. Using Y ( Week year) instead of y (Year). Check the documentation to learn more about it.
  2. Not using Locale with SimpleDateFormat. Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it.
  3. Enclosing Z within single quotes means it is just a character literal with no meaning other than representing the letter Z. The pattern, yyyy-MM-dd'T'HH:mm:ss.SSS'Z' should be yyyy-MM-dd'T'HH:mm:ss.SSSXXX.

Solution using java.time, the modern Date-Time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        var dateString = "2021-05-12T12:12:12.121Z";
        var odt = OffsetDateTime.parse(dateString);
        var dtf = DateTimeFormatter.ofPattern("MMM dd, uuuu", Locale.ENGLISH);
        System.out.println(dtf.format(odt));
    }
}

Output:

May 12, 2021

ONLINE DEMO

Here, you can use y instead of u but I prefer u to y.

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

like image 135
Arvind Kumar Avinash Avatar answered Oct 23 '25 19:10

Arvind Kumar Avinash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!