I want convert string datetime to formatted string. e.g "2018-12-14T09:55:00" to "14.12.2018 09:55" as String => Textview.text
how can I do this with kotlin or java for android ?
I want convert string datetime to formatted string. e.g "2018-12-14T09:55:00" to "14.12.2018 09:55" as String => Textview.text
how can I do this with kotlin or java for android ?
Parse it to LocalDateTime
then format it:
LocalDateTime localDateTime = LocalDateTime.parse("2018-12-14T09:55:00");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String output = formatter.format(localDateTime);
If this does not work with api21, you can use:
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String output = formatter.format(parser.parse("2018-12-14T09:55:00"));
or import ThreeTenABP.
org.threeten.bp.LocalDateTime
and org.threeten.bp.format.DateTimeFormatter
. –
Ticktacktoe DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.forLanguageTag("tr"));
. It’s longer, but avoiding hand-typing the format pattern string is worthwhile, and this also better lends itself to internationalization. –
Ticktacktoe SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())
instead. –
Doubs Kotlin API levels 26 or greater:
val parsedDate = LocalDateTime.parse("2018-12-14T09:55:00", DateTimeFormatter.ISO_DATE_TIME)
val formattedDate = parsedDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"))
Below API levels 26:
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm")
val formattedDate = formatter.format(parser.parse("2018-12-14T09:55:00"))
java.text
or android.icu.text
? –
Wadlinger java.text
, android.icu.text
is just full of stubs, huh!? –
Wadlinger If you have a date-time that represents a value in a specific time zone but the zone is not encoded in the date-time string itself (eg, "2020-01-29T09:14:32.000Z") and you need to display this in the time zone you have (eg, CDT)
val parsed = ZonedDateTime.parse("2020-01-29T09:14:32.000Z", DateTimeFormatter.ISO_DATE_TIME).withZoneSameInstant(ZoneId.of("CDT"))
That parsed
ZoneDateTime will reflect the time zone given. For example, this date would be something like 28 Jan 2020 at 8:32am.
In kotlin u can do this way to format string to date :-
val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.getDefault())
val date = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()).format(simpleDateFormat.parse("2022/02/01 14:23:05")!!)
Should import java.text.SimpleDateFormat For SimpleDateFormat Class to work on api 21
The java.util
date-time API and their corresponding parsing/formatting type, SimpleDateFormat
are outdated and error-prone. In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time
, the modern date-time API.
DateTimeFormatter
to parse your date-time stringjava.time
API is based on ISO 8601 and therefore you do not need a DateTimeFormatter
to parse a date-time string which is already in ISO 8601 format (e.g. your date-time string, 2018-12-14T09:55:00
).
However, your desired output, 14.12.2018 09:55
is not in ISO 8601 standard format and therefore, you need a DateTimeFormatter
to get a string in the desired format.
Demo:
class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.parse("2018-12-14T09:55:00");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu HH:mm", Locale.ENGLISH);
String output = ldt.format(formatter);
System.out.println(output);
}
}
Output:
14.12.2018 09:55
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.
* If you are working on 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.
Here is Kotlin One Liner,It will return empty string if input date cannot be parsed.
fun String.getDateInAnotherFormat(inputFormat: String,outputFormat: String):String = SimpleDateFormat(inputFormat, Locale.getDefault()).parse(this)?.let { SimpleDateFormat(outputFormat,Locale.getDefault()).format(it) }?:""
Usage:
var dateStr = "2000-12-08"
dateStr.getDateInAnotherFormat("yyyy-MM-dd","MMM dd YYYY")
SimpleDateFormat
. –
Ticktacktoe Kotlin: Date time format
fun getApiSurveyResponseDateConvertToLocal(date: String?): String? {
return try {
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
val outputFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
val datee = date?.let { inputFormat.parse(it) }
datee?.let { outputFormat.format(it) }
} catch (e: ParseException) {
e.printStackTrace()
""
}
}
Java: Date time format
public static String getApiSurveyResponseDateConvertToLocal(String date) {
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date datee = inputFormat.parse(date);
return outputFormat.format(datee);
} catch (ParseException e) {
e.printStackTrace();
return "";
}}
Kotlin : Time format converter 24 hrs to 12 hrs
private const val SERVER_TIME_FORMAT = "hh:mm a"
private const val SERVER_TIME_24HR_FORMAT = "HH:mm"
fun getEmpTimeLineFormat(date: String?): String? {
val serverFormat = SimpleDateFormat(
SERVER_TIME_24HR_FORMAT,
Locale.ENGLISH
)
val timelineFormat = SimpleDateFormat(
SERVER_TIME_FORMAT,
Locale.ENGLISH
)
return try {
serverFormat.parse(date)?.let { timelineFormat.format(it) }
} catch (e: ParseException) {
e.printStackTrace()
""
}
}
© 2022 - 2024 — McMap. All rights reserved.