SimpleDateFormat showing a.m./p.m. instead of AM/PM - kotlin

I am trying to use SimpleDateFormat to display the time as 12:00:00 AM. Here is what I am doing:
val timeFormat = SimpleDateFormat("hh:mm:ss a")
val timeString = timeFormat.format(Date(transaction.time))
According to these docs a should show AM/PM
timeString come out in the format: 12:00:00 a.m.
How can I make it show AM instead of a.m.?

Related

convert LocalDateTime to Date by using kotlin

I have a java.time.LocalDateTime and i want to convert it in java.util.Date
val myLocalDateTime = LocalDateTime.now().plusDays(5)
I want to convert LocalDateTime.now().plusDays(5) to java.util.Date in kotlin.
How Can i do it?
You need to add mandatory information in order to make a LocalDateTime convertable to a java.util.Date…
Before you try, think about if you can use Instant.now(), because the methods for legacy compatibility require Instants as arguments:
fun main() {
// get "now" as Instant
val now = Instant.now()
// print the java.util.Date created from the Instant
println(Date.from(now))
// use the Instant plus 5 days to create another date
val date = Date.from(now.plus(5, ChronoUnit.DAYS))
// create a formatter for the Date
val formatter = SimpleDateFormat("yyyy-MM-dd")
// and print it using the formatter
println(formatter.format(date))
}
Output (some moments ago on my middle-european machine):
Thu Feb 09 15:31:06 CET 2023
2023-02-14
If you really need to use a LocalDateTime, which has no zone or offset, you will have to add one, convert the result (a ZonedDateTime or an OffsetDateTime) toInstant() and create Dates from those…
If it's all about dates (year, month of year, day of month) without time of day, you could simply use a java.time.LocalDate:
fun main() {
// get "today" as LocalDate
val today = LocalDate.now()
// print it using toString() implicitly
println(today)
// add five days
val fiveDaysLater = today.plusDays(5)
// and print it formatted by a prebuilt formatter
println(fiveDaysLater.format(DateTimeFormatter.ISO_LOCAL_DATE))
}
Output:
2023-02-10
2023-02-15

How do I return the local time portion of a string given a date string

I am wanting to display in a react native app a date given by the API.
The API is returning the following value:
"2022-06-20T14:00:00.000"
I need to display to the user
2:00 PM
The problem is that when I try this:
const dateString = "2022-06-20T14:00:00.000"
const date = new Date(dateString);
console.log(date)
The date has been changed to:
2022-06-20T14:00:00.000Z
Meaning it is 2pm UTC time when it should be 2pm local time.
On the end of the new Date variable, if you add the .toLocaleTimeString(), it should then output as 2:00:00PM.
const date = new Date(dateString).toLocaleTimeString();
you can use moment library with its localized formats,
here is the documentation for more of its formats https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/
const dateString = "2022-06-20T14:00:00.000"
console.log(moment(dateString).format('LT'))

Convert string into LocalDate Kotlin

I want to convert string value
this is my code like
val dateFirst = "20 Aug 2012"
val dateSecond = "12/16/2020 12:00:00 AM"
val dateFirstConverted = LocalDate.parse(dateFirst, DateTimeFormatter.BASIC_ISO_DATE)
val dateSecondConverted = LocalDate.parse(dateSecond, DateTimeFormatter.BASIC_ISO_DATE)
println(dateFirstConverted)
println(dateSecondConverted)
then i get error like this.
Exception in thread "main" java.time.format.DateTimeParseException: Text '20 Aug 2012' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at App.TryKt.main(try.kt:11)
at App.TryKt.main(try.kt)
can someone help me how to fix this ?
you have problem because the format of the date is not supported, I invite you to read this article https://www.claudebueno.com/programmation/comment-gerer-la-date-et-lheure-avec-kotlin.htm but in your case if you want that the code runs, change the format of date, like this:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.LocalDate
fun main() {
//example
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.BASIC_ISO_DATE
val formatted = current.format(formatter)
println("Current Date is: $formatted")
//your code
val dates = /*"20 Aug 2012"*/ "20120820"
val datess = LocalDate.parse(dates, DateTimeFormatter.BASIC_ISO_DATE)
println(datess)
}
tl;dr ⇒ You are using the wrong pattern for parsing
Your date String is of the format dd MMM uuuu (a non ISO format) but you are trying to parse it with a DateTimeFormatter.ISO_LOCAL_DATE
Your datetime String is of the format MM/dd/uuuu hh:mm:ss a (non ISO) but you are trying to parse it with a DateTimeFormatter.ISO_LOCAL_DATE, which is at least doubly wrong because that formatter tries to parse an ISO date. Your String is non ISO and contains more information (time of day) than this formatter is able to parse.
There are several built-in DateTimeFormatters, like the one you are currently using, but you need to use a correct one or if there is none, create one that covers your String(s) yourself (either by DateTimeFormatter.ofPattern(...) or by using a DateTimeFormatterBuilder).
Here's a small example for your String examples:
fun main(args: Array<String>) {
// your example Strings
val dateFirst = "20 Aug 2012"
val dateSecond = "12/16/2020 12:00:00 AM"
// you need two different formatters here, your Strings differ in format and content
val firstFormatter = DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH)
val secondFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm:ss a", Locale.ENGLISH)
// then use those according to what you want to parse
val localDate = LocalDate.parse(dateFirst, firstFormatter)
val localDateTime = LocalDateTime.parse(dateSecond, secondFormatter)
// use the built-in formatters for output
println(localDate.format(DateTimeFormatter.ISO_LOCAL_DATE))
println(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
}
Output (in ISO):
2012-08-20
2020-12-16T00:00:00

Is there a way to customize the date formatter as per our requirements in swift iOS

I want to display a date in my viewcontroller as January 1-7,2005, which in-turn represents the dates of the current week like 1-7, 8-14.... But i am not finding how to customize the date formatter to have this format.
You can use the DateFormatter() function
let date = Date() // Gets current date
let dateFormatter = DateFormatter()
let monthInt = Calendar.current.component(.month, from: date) // Gets the current month number
let monthStr = Calendar.current.monthSymbols[monthInt-1] // Month to string
dateFormatter.dateFormat = "dd-M,YYYY"
let dateString = dateFormatter.string(from: date)
print("\(monthStr) \(dateString)")
Output: June 29-6,2020
Ensure your dateFormat conforms to ISO 8601 https://www.w3.org/TR/NOTE-datetime

How to set default time to 00:00:00 in kendo Date Time picker

Here is my Kendo datetime picker code
#(Html.Kendo().DateTimePicker()
.Name("start")
.Value(DateTime.Today)
.ParseFormats(new string[] { "MM/dd/yyyy" })
)
by this am getting 12/22/2014 12:00 AM but i want to diplay as 12/22/2014 00:00:00
how can i set default start date to the current day with the time set to 00:00:00
I tried like this but am not getting to display time as 00:00:00
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();
$('#FromDate').data('kendoDateTimePicker').value(month, day, year);
$('#FromDate').data('kendoDateTimePicker').value(new Date("MM/dd/yyyy"));
Thanks for the help
I believe that is what you want: http://dojo.telerik.com/UBaQe ?
So in the C# ASP.MVC this code should work:
#(Html.Kendo().DateTimePicker()
.Name("start")
.Value(DateTime.Today)
.ParseFormats(new string[] { "MM/dd/yyyy" })
.Format("yyyy/MM/dd HH:mm:ss")
.TimeFormat("HH:mm:ss")
)