Date string format convert [duplicate] - java

This question already has answers here:
Changing String date format
(4 answers)
DateTimeFormatter create pattern [duplicate]
(1 answer)
How to check if string matches date pattern using time API?
(3 answers)
Closed 4 months ago.
Date string1: 2022-10-30T04:45:00+00:00
Date string2: Oct 30, 2022 at 04:45 GMT
Date string1 and Date string2, i want formate is "yyyy/MM/dd HH:mm:ss"

Related

java - date to epochtime [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
How to extract epoch from LocalDate and LocalDateTime?
(7 answers)
How to convert a date to milliseconds
(5 answers)
Why am i getting the error "Unparseable date" from SimpleDateFormat?
(3 answers)
Closed 6 days ago.
I am reading a text time format as a string as follows. How can I convert this to unix epoch time in java. Can you help me?
String = "Feb 11 2023 9:35:41 PM"
Example: String = "Feb 11 2023 9:35:41 PM"

How to convert Date String (2021-01-13) to '2020-01-13T00:00:00.000-4:00' format without losing seconds, microseconds [duplicate]

This question already has answers here:
String to ZonedDateTime is changing format
(2 answers)
Closed 1 year ago.
I have used the below code to convert the string to the required format. But in the output seconds, microseconds are missing.
LocalDate date = LocalDat.parse(inputString, DateTimeFormatter.ISO_LOCAL_DATE);
date.atStartOfDay(ZooneId.of("America/New_York").toOffsetDateTime().toString();
Output based on the above code: '2021-01-13T00:00-4:00'
Required output: '2020-01-13T00:00:00.000-4:00'
You can further reformat it using the format() method.
LocalDate date = LocalDate.parse("2021-01-13", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date.atStartOfDay(ZoneId.of("America/New_York")).toOffsetDateTime().format(DateTimeFormatter.ofPattern(("yyyy-MM-dd'T'HH:mm:ss:SSSZ"))));
see full list of available date formats here
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

In Java how can I format a Time of HHMM to HH:MM AM/PM [duplicate]

This question already has answers here:
How to convert 24 hr format time in to 12 hr Format?
(16 answers)
Closed 2 years ago.
I have a time in the format HHMM (1643) and I want it to be formatted to HH:MM AM/PM (04:43 PM)
How can I achieve this in Java?
I'm doing something similar for the date which is formatted YYYMMDD (20201013) into MM/DD/YYY (10/13/2020) with this code:
new java.text.SimpleDateFormat("yyyyMMdd").parse($F{date})
You can use DateTimeFormatter & LocalTime from java.time
First, parse the time as LocalTime using DateTimeFormatter. Then again format the LocalTime into String in your desire format.
LocalTime time = LocalTime.parse("1643", DateTimeFormatter.ofPattern("HHmm"));
String formattedTime = time.format(DateTimeFormatter.ofPattern("hh:mm a"));
System.err.println(formattedTime);
Output: 04:43 PM

Timestamp string to Date [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
How to convert the string to a date
In the string we have a timestamp (for example "2018-07-11T04:40:30Z"),
and I want to simply convert this String time to 11-07-2018 16:40:30
String Time = jresponse.getString("timestamp");
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = sf.parse(Time);
I tried many times this code and it doesn't work because I have always this format "2018-07-11T04:40:30Z" and not this 11-07-2018 16:40:30 why ?
If you want to parse dates like "2018-07-11T04:40:30Z"then you should change the format as "dd-MM-yyyy HH:mm:ss" will never work with that date. The correct format should be something like yyyy-MM-dd'T'HH:mm:ssZ.

How to convert date string to Date [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have the following string: Feb 16, 2014 2:11:41 PM
I have Googled around for some time now and only see examples on converting strings in the format mmddyyyy. I can't seem to find out how to convert the format above.
How can I convert this to a Date.
Thanks,
Gary
Use below formatter:
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd,yyyy HH:mm:ss aa");
Also this link would be very helpful

Categories