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

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.

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.

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

Get current date in YYYY-MM-DD format in Java (Android) [duplicate]

This question already has answers here:
Month issue in SimpleDateFormat class
(6 answers)
Closed 5 years ago.
I am trying to get a Date object in the format YYYY-MM-DD representing the current system date. Below is my attempt:
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String todayString = formatter.format(todayDate);
The values are as follows:
todayDate: Mon May 15 16:24:47 GMT+01:00 2017
todayString: 2017-24-15
Having tried this a couple of times I noticed the todayString is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.
How might I get the current date in the YYYY-MM-DD format?
Change this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
to this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
mm stands for the minutes, while MM (capitalized) stands for the month

How to get Date in the same format as String [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I want to get the Date object which contains Date in the form yyyy-mm-dd after adding few months to existing Date object.
Using DateFormat object I have tried this way but its not giving the output as I want. How can I get this Date format in Java?
My code-
Calendar c=Calendar.getInstance();
DateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
Date d=cal.getTime();
c.add(Calendar.MONTH,10);
Date newd1=c.getTime();
String news=sdf.format(newd1);
Date dnew=(Date)sdf.parse(news);
String news has the date of format yyyy-mm-dd but when I use parse on that String it results Date object which is of format "Thu Jan 21 00:14:00 IST 2016". How can I get the Date object in the form of "yyyy-mm-dd" using String news in the above code.
java.util.Date doesn't have format. Its just Date.
You can make it print its values in any form you want, by using SimpleDateFormat
You can try as this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String news = sdf.format(new Date());
//for testing
System.out.println(news);

Categories

Resources