How to convert sql date format? [duplicate] - java

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.

Related

SimpleDateFormat.format(currentDate) does not change currentDate variable [duplicate]

This question already has answers here:
Convert String date into java.util.Date in the dd/MM/yyyy format [duplicate]
(1 answer)
Java Date changing format [duplicate]
(4 answers)
Format a Date and returning a Date, not a String
(11 answers)
Closed 2 years ago.
when i make something like this in my servlet im still getting date format like Sun Jun 07 00:59:46 CEST 2020 but i want to make it like 2020.06.07
Code in my servlet :
SimpleDateFormat sdfo = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date();
sdfo.format(currentDate);
request.setAttribute("currentDate", currentDate);
And my jsp file :
Current date: <c:out value="${currentDate}"/>
What i should change here?
You have set the currentDate instead of the formatted date string into request.
Replace
sdfo.format(currentDate);
request.setAttribute("currentDate", currentDate);
with
String today = sdfo.format(currentDate);
request.setAttribute("currentDate", today);
I also recommend you use java.time.LocalDate and DateTimeFormatter.ofPattern instead of using the outdated java.util.Date and SimpleDateFormat as shown below:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate currentDate = LocalDate.now();
String today = currentDate.format(formatter);
request.setAttribute("currentDate", today);
Check this to learn more about the modern date/time API.

Parsing of 24hr date format doesn't work as expected when time is in 12th hr(12-1 PM) [duplicate]

This question already has answers here:
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
java to mysql. I need convert from string parametre to timestamp
(2 answers)
Convert date into AEST using java
(2 answers)
Closed 3 years ago.
I am using below code to parse text to date datatype and get respective long value.
import java.text.DateFormat
import java.text.SimpleDateFormat
String TIMEZONE_DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ssZ";
DateFormat df = new SimpleDateFormat(TIMEZONE_DATE_FORMAT);
Date date = df.parse("2015-09-21T12:48:00+0000");
date.getTime();
In the above case I get getTime() value as: 1442796480000 which is 12:48 AM(GMT). But I am expecting 12:48 PM as its 24 hr format.
When I use the same code with text date: 2015-09-21T13:48:00+0000, I get 1:48 PM which is correct.
Am I using the wrong date format?
For 24h the pattern should be HH instead hh
Try this:
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
It worked fine for me! :)

Is it possible to get Date object in Android in dd-mm-yyyy format? [duplicate]

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

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

How to parse this date in java : "12/19/2015 8:00:00 AM" [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 7 years ago.
I'm new to java and I'm getting problem parsing this date: "12/19/2015 8:00:00 AM".
Just follow the documentation and use the formatting elements that correspond to the string you have:
String strDate = "12/19/2015 8:00:00 AM";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date date = df.parse(strDate);

Categories

Resources