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

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?

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.

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.

Android: Parsing datetime with timezone using DateTimeFormatter [duplicate]

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

Java Date Format from yyyy-MM-dd'T'HH:mm:ss.SSS+ZZZZ [duplicate]

This question already has answers here:
Cannot parse String in ISO 8601 format, lacking colon in offset, to Java 8 Date
(3 answers)
Convert date into AEST using java
(2 answers)
Java string to date conversion
(17 answers)
Closed 3 years ago.
Formatting the date somehow is giving me an exception.
The date string that I want to convert to a Date object => 2019-12-12T15:31:31.000+0000
Code for converting which gives the error Text '2019-12-12T15:31:31.000+0000' could not be parsed at index 24
Date date;
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'+'Z");
ZonedDateTime formatDateTime = ZonedDateTime.parse(dateString,formatter);
date = Date.from(formatDateTime.toInstant());
return date.toString();
}catch (Exception ex){
return ex.getLocalizedMessage();
}
I'm not sure why this isn't working, the Date has been a huge problem for me in Java. In C# there's hardly any work that needs to be done but I couldn't figure out how to parse a date string in Java.
Help is appreciated, thanks people.

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.

Categories

Resources