Android: Parsing datetime with timezone using DateTimeFormatter [duplicate] - java

This question already has answers here:
Parse String to date with timezone offset intact
(1 answer)
Java date offset format issue?
(4 answers)
DateTimeFormatter create pattern [duplicate]
(1 answer)
Closed 2 years ago.
Convert string '2018-08-27T16:40:02+08:00' to datetime using DateTimeFormatter
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
String rawDateString = "2018-08-27T16:40:02+08:00"
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern(DATE_TIME_FORMAT, Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse(rawDateString, dateTimeFormatter);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
but this returns DateTimeParseException: Text could not be parsed at index 19
I saw some similar issue like this
DateTimeParseException: Text could not be parsed at index 2
but I dont see any sample with timezone on datetime format.

You neeed to specify timezone offset in your format as well. Like that:
String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssXXX";
See the link bellow:
https://www.journaldev.com/17899/java-simpledateformat-java-date-format

Related

Converting String to LocalDate object in Java [duplicate]

This question already has answers here:
java.time.format.DateTimeParseException, Date could not be parsed
(1 answer)
Create LocalDate Object from Integers
(3 answers)
Invalid localdate when it created
(1 answer)
Can't parse String to LocalDate (Java 8)
(2 answers)
Difference between year-of-era and week-based-year?
(7 answers)
Closed 3 months ago.
I want to convert date from String to LocalDate object: Starting date as String: "12/11/2022" I want to convert this String to LocalDate. I want to get LocalDate object and next use this LocalData inside if-else conditions to convert this object to String and return String. All conditions return first LocalData and next String as result: if I use this code with: date = LocalDate.parse(default)};
public String getStringDate(String timeProjection){
if (timeProjection.equals("1") {
date = LocalDate.now();
}
....
else {
String default = "12/11/2022";
date = LocalDate.parse(default);
}
command
return date.format(DataTimeFormatter.ofPattern("dd/MM/YYYY"));
I get issue that is impossible this parse, but when I use:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
String date = "16/08/2016";
LocalDate localDate = LocalDate.parse(date, formatter);
This convertion works good. What do you think is possible create solution when I can't use twice this formatter: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY") - first in else body, second in result command? Thanks for your help.

I cannot convert C# datetime to java date time [duplicate]

This question already has answers here:
Why am I getting a parse exception when I try to parse the current LocalDateTime [duplicate]
(2 answers)
How to convert a string date "2019-04-21T12:08:35" to SimpleDateFormat, there by convert to Date?
(2 answers)
Can’t rid of 'T' in LocalDateTime
(3 answers)
Closed 1 year ago.
I get a datetime string from an C# API that looks like this "2021-05-07T08:58:15.993". I am trying to convert it to "2021-05-07 08:58 AM" for my android app. I am using this code:
fun convertStringToDate(stringDate: String): LocalDate? {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm ", Locale.ENGLISH)
val date = LocalDate.parse(stringDate, formatter)
return date
}
but it gives me an error of DateTimeParseException "2021-05-07T08:58:15.993" could not be parsed at index 10.
Index 10 is the 'T' character. I could just hack it and just remove the T and remove the .993, but, what if the date format changes from the API?

How to convert sql date format? [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Convert String date into java.util.Date in the dd/MM/yyyy format [duplicate]
(1 answer)
SimpleDateFormatter.parse giving output in different format than specified
(1 answer)
Android SimpleDateFormat, how to use it?
(11 answers)
Closed 2 years ago.
Hello I am trying to convert sql date format to "dd-MM-yyyy" but in the output I have this unwanted format which shows the current date time (Mon Aug 14 00.00....)
String inputDate = rs.getDate("BIRTH_DATE").toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date outputDate = formatter.parse(inputDate);
user.setBirthdate(outputDate);
Dates do not have an inherent format.
java.util.Date inputDate = rs.getDate("BIRTH_DATE");
user.setBirthdate(inputDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String representation = formatter.format(inputDate);
It would be better to use the new java.time classes: LocalDate, ZonedDateTime and so on.

How to convert Russian date format to US date format? [duplicate]

This question already has answers here:
How can I parse Date format in Java with different locale [duplicate]
(2 answers)
Proper Russian month string translation Java
(6 answers)
Changing String date format
(4 answers)
Closed 3 years ago.
i want to change Russian Date to US format
String rDate = "28 августа 2017";
output :
String usDate = "28-08-2017";
You can use DateTimeFormatter with withLocale.
Parsing text.
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter
.ofPattern("dd MMMM uuuu")
.withLocale( new Locale("ru") )
;
LocalDate ld = LocalDate.parse(rDate, dateTimeFormatter);
Generating text.
String usDate = ld.format(DateTimeFormatter.ofPattern("dd-MM-uuuu"));
=> 28-08-2017
See this code run live at IdeOne.com.

How i can get date format of any datetime string? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 4 years ago.
I receive this "10/1/2018, 1:27:42 PM" as date from server. Now want to convert it to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). But to do this i need to know the format of existing date string.
How i can find a correct format of "10/1/2018, 1:27:42 PM"?
You need to use SimpleDateFormat.toPattern() to get the pattern. Use this function in this way -
SimpleDateFormat sdf = new SimpleDateFormat();
// get the current date and time
Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime());
// get the pattern used by the formatter and print it
String pattern = sdf.toPattern();
System.out.println("Pattern:"+pattern);

Categories

Resources