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

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));

Related

JAVA Date conversion from "hh:mm aa" does not list correct time [duplicate]

This question already has answers here:
Comparing two times in android
(4 answers)
Convert String to java.util.Date
(4 answers)
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
Closed 1 year ago.
I am a little confused about what I see with the following test code:
public class TheDateIssue {
public static void main(String[] args) {
String TIME_FORMAT = "hh:mm aa";
try {
Date theDate = new SimpleDateFormat(TIME_FORMAT).parse("13:15 pm");
System.out.println("theDate: " + theDate);
} catch (ParseException e) {
System.out.println("Exception while converting string to Date. \n" + e.getLocalizedMessage());
}
}
The time to be parsed is "13:15 pm" - but the sysout output lists the time (highlighted below). I was expecting either 13:15:00 PST or 01:15:00 pm PST
theDate: Fri Jan 02 **01:15:00 PST** 1970
What am I doing wrong? :(
Try this:
LocalTime time = LocalTime.parse("13:15")
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm a")));
Or:
ZonedDateTime time = ZonedDateTime.of(LocalDate.now(), LocalTime.parse("13:15"), ZoneId.of("America/Los_Angeles"));
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));
And if for some reason you are stuck with a date class, transform it somehow:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ssZ aa");
Date theDate = simpleDateFormat.parse("13:15:00 PST PM");
ZonedDateTime date = ZonedDateTime.ofInstant(theDate.toInstant(), ZoneId.of("America/Los_Angeles"));
System.out.println(date.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));
But you are right, it is weird behaviour. There are good reasons they created LocalDate etc, moving away from the Date class.

Java date format dd.mm.yyyy [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 1 year ago.
I have to parse String to Date format(dd.MM.YYYY).
Here is my code:
Date date = null;
try {
date = new SimpleDateFormat("dd.MM.yyyy").parse(regDate);
} catch (Exception e) {
System.out.println(e);
}
System.out.println(date);
But the output is: Tue Oct 12 00:00:00 EEST 2021. How can I change my code to print the date in the required format?
Try By using - or / in place of "."
date = new SimpleDateFormat("dd-MM-yyyy").parse(regDate);
or
date = new SimpleDateFormat("dd/MM/yyyy").parse(regDate);

return format yyyy-mm-dd HH:mm:ss(String) to java Date [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I have a function that needs to return a Date, I have a Date String of
'2016-02-09T12:22:00.000+00:00'
and want to convert it to
'2016-02-09 12:22:00'
but my return result is a string, in my case i want this to be a date. Here is my function so far i have tried is:
private Date parse(String s){ // function needs to return date as yyyy-mm-dd HH:mm:ss
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
String formattedTime = output.format(d);
System.out.println("Formated Date is = "+ formattedTime); // return date in yyyy-mm-dd HH:mm:ss (but a string)
return date; (Here i want the expected date to be as yyyy-mm-dd HH:mm:ss, I am getting as 'Tue Feb 09 12:22:00 GMT+05:30 2016')
}
Please help me its been a while trying this and am failing every time.
Thanks.
Date has no any format, it's just... date. yyyy-mm-dd HH:mm:ss is string representation according to some format (your code has this), 'Tue Feb 09 12:22:00 GMT+05:30 2016' is result of default toString().
Just parse string into Date object and return it, then call SimpleDateFormat.format(date) wherever it needs to be showed / logged / whatever.

How to display date in dd/mm/yyyy format? [duplicate]

This question already has answers here:
Convert java.util.Date to String
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 8 years ago.
I am to new to Java. I want to display date in dd/mm/yyyy format. When I print date using new Date() function it gives me Sting which I don't want. Say I want to print today's date it gives me Sat Jan 24 08:17:41 IST 2015 but I want output as 24/1/2015
Use SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
There you have one way to do it: http://docs.oracle.com/javase/tutorial/datetime/iso/format.html
So basically you can define the format in an formatter and use that to convert
the date into a string:
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
String out = departure.format(format);
System.out.printf("LEAVING: %s (%s)%n", out, leavingZone);
}
catch (DateTimeException exc) {
System.out.printf("%s can't be formatted!%n", departure);
throw exc;
}

How to parse a Date+Time String [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
Using java I need to parse this 2014-08-31 13:53:42.0 to 31-AUG-14 01.53.42 PM
Start by doing some research into java.text.SimpleDateFormat which can be used to parse String values of a verity of formats into a java.util.Date.
You can then use another SimpleDateFormat to format the value to the format that you want, for example
try {
String in = "2014-08-31 13:53:42.0";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = sdfIn.parse(in);
System.out.println(date);
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MMM-yy hh:mm.ss a");
System.out.println(sdfOut.format(date));
} catch (ParseException ex) {
}
Which outputs
Sun Aug 31 13:53:42 EST 2014
31-Aug-14 01:53.42 PM

Categories

Resources