Java - Formatting and get name of day: Date object [duplicate] - java

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
All
When I define a date object it automatically assumes it's MM/dd/YYYY, however I enter it as dd/MM/yyyy.
Date d1 = new Date("5/12/2015 10:30:00");
When I get the name of the day from that date it gives me the name of May 12 (Tuesday) instead of the name of december 5 (Saturday).
dateName = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(d1);
Can anyone help me out?

You can use a SimpleDateFormat formatter for that.
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = format.parse("05/12/2015");
With that you can define your own format-strings!

Related

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

in java, how to convert String to Date object "yyyy-MM-dd'T'HH:mm" [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
i want to convert String "yyyy-MM-dd'T'HH:mm" (for ex,2015-04-13T10:00:00) into date object with the same format(yyyy-MM-dd'T'HH:mm). Please let me know how to do this.
String dateString = "2015-04-13T10:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
Date date = simpleDateFormat.parse(dateString);
Now if you want to display the date in the same format, you can do the following
System.out.println(simpleDateFormat.format(date)); // Outputs "2015-04-13T10:00"

SimpleDateFormat mmddyyyy returns improper data for month [duplicate]

This question already has answers here:
getting month name wrong with SimpleDateFormat
(2 answers)
Closed 9 years ago.
I want a timestamp field. I tried the below to get time in mmddyyyyhhmmss format.
All the fields give proper data but the month field has improper value.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mmddyyyyhhmmss");
String date = simpleDateFormat.format(new Date());
O/P I get is 13132013021331. What I expect is 03133013021331.
Your Month should be MM not mm
mm means Minutes
SimpleDateFormat mmddyyyy returns improper data for month
coz your format for month is wrong.
it should be:
new SimpleDateFormat("MMddyyyyhhmmss");
MM---> Month
mm---> minutes
You need this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMddyyyyhhmmss");
String date = simpleDateFormat.format(new Date());
You used "mm" twice - once for month (incorrect) and again for minutes (correct).

Parsing date gives month of 00 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Improper result of SimpleDateFormat
The following code outputs a date of 00/12/2010. I can't figure out why the date string is parsed without exception but the month is set to 00. I tried using a second argument to the SimpleDateFormat constructor of Locale.Enlgish for sanity's sake but that didn't help. Any suggestions?
String dateString = "12-OCT-10";
SimpleDateFormat formatFrom = new SimpleDateFormat("dd-MMM-yy");
Date date = formatFrom.parse(dateString);
SimpleDateFormat formatTo = new SimpleDateFormat("mm/dd/yyyy");
System.out.println(formatTo.format(date));
The mm is for minute, use MM; see the documentation for details.

Categories

Resources