In my application I one string such as 2023-2-14 and I want convert this to 2023-02-14.
I write below codes:
val format = SimpleDateFormat("yyyy-MM-dd")
val date: Date = format.parse(startDateStr)
Log.e("dateLog",""+date)
But in logcat show me this : Wed Feb 15 00:00:00 GMT+03:30 2023
Why? I used this format : yyyy-MM-dd.
Why not used this format?
you are just parsing date, without a time, thus date object have set 00 for hour, day etc. now use format method for converting old String to new one
val formatAs = "yyyy-MM-dd"
var format = SimpleDateFormat(formatAs)
val date: Date = format.parse(startDateStr)
Log.e("dateLog","date:"+date)
String dateAsStringFormatted = format.format(date);
Log.e("dateLog","dateAsStringFormatted:"+dateAsStringFormatted)
some other answers in HERE
Related
This question already has answers here:
How to get current time from internet in android
(8 answers)
Closed 3 days ago.
i want to get the date and time without using the systems date
i am using android studio emulator changing emulators date.
ex: todays date is 2 Feb 2023 but i change my devices date to 26 Feb 2023. i still want it to display 2 Feb 2023 of todays date without depending on systems/device date
this is my code
var dateTime: String
val timeZone = TimeZone.getTimeZone("GMT+8")
val calendar = Calendar.getInstance(timeZone)
val simpleDateFormat = SimpleDateFormat("d MMM yyyy", Locale.US)
simpleDateFormat.setTimeZone(timeZone)
dateTime = simpleDateFormat.format(calendar.getTime()).toString()
but it still returns depending on the systems date
Yes, it is possible but you need to make a network call for it
https://worldtimeapi.org/api/timezone/Asia/Kolkata
it will give you JSON like this
{
"abbreviation": "IST",
"client_ip": "122.170.105.143",
"datetime": "2023-02-15T10:23:26.414176+05:30",
"day_of_week": 3,
"day_of_year": 46,
"dst": false,
"dst_from": null,
"dst_offset": 0,
"dst_until": null,
"raw_offset": 19800,
"timezone": "Asia/Kolkata",
"unixtime": 1676436806,
"utc_datetime": "2023-02-15T04:53:26.414176+00:00",
"utc_offset": "+05:30",
"week_number": 7
}
Once you have JSON in the response variable you can parse it like
var response; //it will contains response
var datetime = response.getString("datetime");
//if you want to parse it in readable format do it like this
val informat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
val outFormat = SimpleDateFormat("dd/MM/yyyy hh:mm:s a")
outputString = outFormat.format(informat.parse(datetime))
you may need to execute the network call in IO Thread you can use this
import in your gradle
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
//your code
CoroutineScope(Dispatchers.IO).launch {
var response =
JSONObject(URL("https://worldtimeapi.org/api/timezone/Asia/Kolkata").readText())
val datetime = response.getString("datetime")
val informat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
val outFormat = SimpleDateFormat("dd/MM/yyyy hh:mm:s a")
var outputString = outFormat.format(informat.parse(datetime))
binding.text.text = outputString
}
I want to parse the following String :
2022-02-14T04:40:33.000000Z
So I'll be able to convert it to this date format:
14/Feb/2022 04:40 or dd/MMM/yyyy HH:mm.
But it throws an exception :
Caused by: java.text.ParseException: Unparseable date: "2022-02-14T04:40:33.000000Z"
My code is a basic SimpleDateFormat with the following code :
val datetime = "2022-02-14T04:40:33.000000Z"
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val outputFormat = SimpleDateFormat("dd/MMM/yyyy HH:mm", Locale.getDefault())
val inputDate = inputFormat.parse(date) // exception thrown here.
val dateString = outputFormat.format(inputDate)
I had tried the following format :
yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ss.SSS'000'Z (I was expecting to ignore the extra 000)
I had read about the PHP DateTime::format which had the u format character for microseconds.
But, the android SimpleDateFormat had no support for the microseconds character.
Please use following format
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
How to convert date into firebase timestamp format in android
My date is - 01-01-2021
Result Format I need is - January 01, 2021 at 3:37:59 PM UTC+5:30(firebase timestamp format)
Thanks in advance
Date: Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as
"the epoch", namely January 1, 1970, 00:00:00 GMT.
import java.util.Date
Simply use Date() as val date = Date() (firebase timestamp format)
while fetching data from firebase use below code
val date = getReadableDateTime(document.getDate("timestamp")) // timpestamp is the key inside your document
private fun getReadableDateTime(date: Date): String {
return SimpleDateFormat("MMMM dd, yyyy - hh:mm a", Locale.getDefault()).format(date)
}
I'm trying to find out how I can convert DateTime to 10 digit timestamp in Kotlin (Android Studio), I can't find any equivalent of it in Kotlin.
For example:
I have a val with date-time value like :
val dateTime = "2020-12-13 17:54:00"
Now I want to convert it to 10 digit timestamp like "1607842496"
Please give me a simple sample to show how I can resolve this problem. Thanks in advance.
Use the SimpleDateFormat class to parse your date String to a Date object. You can then get the timestamp (in milliseconds) from that Date object like so:
val dateTime = "2020-12-13 17:54:00"
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val date = simpleDateFormat.parse(dateTime)
val timestamp = date?.time
Divide the timestamp by a 1000 to get the 10 digit (in seconds) timestamp.
I use ThreeTen module and when I print ZonedDateTime.now(), I get.
2019-07-11T22:43:36.564-07:00[America/Los_Angeles]
What's the format of this?
I tried uuuu-MM-dd'T'HH:mm:ss.SSS'Z' and It says,
org.threeten.bp.format.DateTimeParseException: Text '2019-07-11T22:43:36.564-07:00[America/Los_Angeles]' could not be parsed at index 23
So, after SSS, the 'Z' part is incorrect.
What's the proper way to implement it?
This is my code:
val pstTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toString()
val timeFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'")
val mTime = LocalDateTime.parse(pstTime, timeFormatter).toString()
tv_pstTime.text = mTime
I want to parse it to the format like Tuesday, July 2 5:15:01 P.M.
How can I do that?
You can use DateTimeFormatter.ofPattern("...."). Inside .ofPattern("....") you can have any pattern you want.
Like this:
val result = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"))
.format(DateTimeFormatter.ofPattern("EEEE, MMMM d HH:mm:ss a"))
Output: Thursday, July 11 23:51:21 PM