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);
Related
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.
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
return date type with format in java [duplicate]
(1 answer)
Closed 4 years ago.
In Android all Date objects are given in this type of date format "Mon Dec 03 00:13:21 GMT+05:30 2018" when you convert it with SimpleDateFormat.
Is there any possibility of getting a Date object in the "dd-mm-yyyy" format in Android?
You could set the pattern on a SimpleDateFormat and format the date:
new SimpleDateFormat("dd-mm-yyyy").format(new Date());
You could convert Strings into Dates as the following:
String pattern = "dd-mm-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String strDate = simpleDateFormat.format(new Date());//firstly get formatted str
Date newDate = simpleDateFormat.parse(strDate);//then get date object with wanted format
This question already has answers here:
converting date format
(5 answers)
Closed 6 years ago.
I have a situation where i can get only the date like 9/2/2016. From this, i need to parse and create a pattern like M/D/YYYY. From this i can convert the any input date to the above pattern M/D/YYYY.
Is there any utility/API for that in java.?
For your requirement, first you parse the input string with appropriate format. Then you do format the date. For more info refer SimpleDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
Date myDate = dateFormat.parse("9/2/2016");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(dateFormat2.format(myDate));
Output
09/02/2016
You can use SimpleDateFormat to do this.
SimpleDateFormat dateFormat = new SimpleDateFormat(MM/dd/yyyy);
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
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!
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"