Java SimpleDataFormat not displaying correctly [duplicate] - java

This question already has answers here:
JAVA SimpleDateFormat returns wrong format [duplicate]
(1 answer)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 2 years ago.
I am trying to convert an array of strings to a Date ArrayList with the following format - "dd/MM/yyyy" I have included my method below. When I print the ArrayList the dates are not formatted correctly and are displayed as: "Thu Mar 05 00:00:00 GMT 2020" Can anyone think why this is happening?
private void convertDates()
{
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");
for (String dateString : date) {
try {
dateList.add(formatter1.parse(dateString));
} catch (ParseException e) {
e.printStackTrace();
}
}
displayDates.setText(Arrays.toString(dateList.toArray()));
}

You have a list of Date objects, and when you format them to a string, they'll use their default formatting, regardless of the format you used to parse them. If you want to display them in that format, you'll have to do so explicitly. E.g.:
displayDates.setText(
dateList.stream().map(formatter1::format).collect(Collectors.joining(","));

Related

In java Trying to convert "2020-11-22T05:57:10+01:00" into Java Date object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
simpledate formater to format returns incorrect.deducted date after formating [duplicate]
(2 answers)
Closed 2 years ago.
try {
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss+hh:mm");
Date dateTime = format .parse("2020-11-22T05:57:10+01:00");
System.out.println(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
Above out put is Sun Dec 29 05:00:10 IST 2019 the 57 minutes part is lost.
The above date I am receiving from JSON response with TimeZone Europe/Berlin.
Currently I haven't include TimeZone. What is correct way to address this string while converting into Java Date object?
Does the above conversion is correct ?
If not then how to convert this data ?

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 "2019-10-11T04:56:06.000Z" to timestamp in Java? [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
How to validate the DateTime string format "2018-01-22T18:23:00.000Z" in Java?
(2 answers)
Android Studio Convert ISO string to "America/New_York" when adding to event to calendar
(1 answer)
Closed 3 years ago.
I have a string "2019-10-11T04:56:06.000Z", how to convert it to timestamp in Java.
You first convert the string to a date (java.util.Date) using the parse method of the SimpleDataFormat class. After, from the date object you can get the timestamp.
String strDate = "2019-10-11T04:56:06.000Z";
try {
Date date=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(strDate);
long timestamp = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}

Format Date ddmmyy Java [duplicate]

This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Java string to date conversion
(17 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
display Java.util.Date in a specific format
(11 answers)
Java Android Calendar/DateFormat this format XXXXXXXX [closed]
(4 answers)
Closed 3 years ago.
I need convert String to Date with next format:
String date1 ="26032018";
// ddmmyyyy
I need:
032618
//mmddyy
I tried:
Date date2 = new SimpleDateFormat("ddmmyy").parse(date1);
But don't work
Output:
Thu Jan 03 00:26:00 CLST 2008
Any idea?
Works for me
String date1 ="26032018";
Date date2 = null;
try {
date2 = new SimpleDateFormat("ddMMyyyy").parse(date1);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("MMddyy").format(date2));
String date1 ="26032018";
String pattern ="MMddyyyy";
Date formatedDate = new SimpleDateFormat("ddMMyyyy").parse(date1);
String d = new SimpleDateFormat(pattern).format(formatedDate);

Why SimpleDateFormat does not apply specified date pattern? [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I have following code to format a date, but the output does not match with the pattern.
try{
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
System.err.println(convertedDate.toString());
}catch(ParseException p){
System.err.println(p.getMessage());
}
Output
Sat Nov 01 00:00:00 EST 2014
I think you are questioning why you don't get the time, that's because your format doesn't include it. I suggest you use "yyyy-MM-dd'T'HH:mm:ss" to match your input and "yyyy-MM-dd" when you call DateFormat#format(Date),
try {
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(date);
System.out.println(convertedDate.toString());
// Per your comment you wanted to `format()` it like -
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(convertedDate));
} catch (ParseException p) {
System.err.println(p.getMessage());
}
For what you described below in our conversation, you can simply do this:
System.err.println((new SimpleDateFormat("yyyy-MM-dd")).format(convertedDate));

Categories

Resources