private fun joinedDate(date: String): String {
val a = LocalDateTime.parse(
date,
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.withLocale(Locale.getDefault())
)
val date = "${a.month.toString().lowercase().replaceFirstChar { it.uppercase() }}, ${a.year}"
return date
}
This is my code i need to show month short name (e.g. Jan for January)
CodePudding user response:
You can use the TextStyle property of the Month enum in the java.time package to show the short month name. Here is an example of how you can update your code to show the short month name:
private fun joinedDate(date: String): String {
val a = LocalDateTime.parse(
date,
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.withLocale(Locale.getDefault())
)
val monthName = a.month.getDisplayName(TextStyle.SHORT, Locale.getDefault())
val date = "$monthName, ${a.year}"
return date
}
In this example, the getDisplayName method is used to get the short month name, which is passed as the first argument to the method. The second argument is the locale you want to use. In this case, it is the default locale.
CodePudding user response:
You can build and use a suitable DateTimeFormatter
that only prints the short month-of-year, a comma and the year…
val dtfOut = DateTimeFormatter.ofPattern(
"MMM, uuuu", Locale.ENGLISH
)
… and then use it in the method LocalDateTime.format(DateTimeFormatter)
.
It will print the desired result if the object actually has a month-of-year and a year.
If you don't know the offset format, use a DateTimeFormatterBuilder
in order to build up a maximally flexible DateTimeFormatter
for parsing. Otherwise just use the one shown in your question.
Full example:
import java.util.Locale
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
/*
define the output formatter
(short month, year)
*/
val dtfOut = DateTimeFormatter.ofPattern("MMM, uuuu", Locale.ENGLISH)
/*
prepare a formatter that is able to parse many
different offsets and no offset at all
*/
val dtfIn = DateTimeFormatterBuilder()
.optionalStart()
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendOffsetId()
.optionalEnd()
.appendOptional(
DateTimeFormatter.ofPattern(
"uuuu-MM-dd'T'HH:mm:ss.SSSX"
)
)
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.toFormatter(Locale.ENGLISH);
// conversion method
private fun joinedDate(date: String): String {
// just parse, format and return
return LocalDateTime.parse(date, dtfIn)
.format(dtfOut)
}
// try with some different input values
fun main() {
val someDateTimes = listOf(
"2022-10-18T21:43:10.111 00",
"2022-10-18T21:43:10.111 0000",
"2022-10-18T21:43:10.111 00:00",
"2022-10-18T21:43:10.111Z",
"2022-10-18T21:43:10.111"
)
someDateTimes.forEach { println("${joinedDate(it)} <-- $it") }
}
Output:
Oct, 2022 <-- 2022-10-18T21:43:10.111 00
Oct, 2022 <-- 2022-10-18T21:43:10.111 0000
Oct, 2022 <-- 2022-10-18T21:43:10.111 00:00
Oct, 2022 <-- 2022-10-18T21:43:10.111Z
Oct, 2022 <-- 2022-10-18T21:43:10.111