Android(java) wrong datetime day [duplicate] - java

This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 1 year ago.
In my app, I noticed that the day from the datetime is wrong
By using:
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
final Calendar cal = Calendar.getInstance();
date = dateFormat.format(cal.getTime());
It gives me 2021-02-49 09:41:29, as you can see. the day from the datetime is 49. Why is it like that?

Related

Unable to format timestamp as YYYY-MM-DD HH:mm:ss in java [duplicate]

This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 4 years ago.
I have a timestamp 13 Dec 2018 19:37:18 which I need to convert to 2018-12-13 19:37:18, I am following the below steps but it is giving incorrect timestamp
DateFormat outputFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("DD MMM YYYY HH:mm:ss");
Date date = inputFormat.parse(updatedStartime);
String NewStartTime = outputFormat.format(date);
I am getting the output as 2018-Jan-01 19:37:18, Do I need to convert the MMM to integer month value before formatting the output? what is the correct step to get the expected output?
D is "day in year"; not "day in month" what you actually need, so d.
Y is "week years"; i guess you meant y for normal year
So in all that would be:
DateFormat outputFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
For more details look at the javadoc https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat Java dates [duplicate]

This question already has answers here:
Java SimpleDateFormat Parse Wrong Date
(2 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 5 years ago.
Any idea what is happening to this code that the dates do not coincide? First prints "20 December 2017" and then "Date with new format:2017-01-01"...
SimpleDateFormat formatter = new SimpleDateFormat("d MMMM YYYY");
SimpleDateFormat formatterNew = new SimpleDateFormat("yyyy-MM-dd");
try{
System.out.println(formatter.format(new Date()));
Date date = formatter.parse("20 December 2017");
System.out.println("Date with new format:" +formatterNew.format(date));
}catch(Exception ex){
ex.printStackTrace();
}
This is related to the use of yyyy vs YYYY. If you change your code to use yyyy it will work.
Here is the explanation of the difference between the two.
Week year

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

Modify a date object to another format [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
I downloaded date information from an API which is in the format of (Java)
day month monthnumber hour:minute:second EST year
how can I format this into just
month monthnumber year
You could use SimpleDateFormat to first parse the string to a java.util.Date, and then format it to the format you want:
String orig = "Thu Feb 19 19:40:12 EST 2015";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = parser.parse(orig);
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
String formatted = formatter.format(date);

Convert Date to String in JAVA [duplicate]

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Java string to date conversion
(17 answers)
Closed 8 years ago.
I have java.util.Date 02/26/2015. I want to convert it like Feburary 26,2015 as String. How can I convert it like that ??
like what goes here ??
DateFormat formatter = new SimpleDateFormat("here");
Use this format:
DateFormat formatter = new SimpleDateFormat("EEE dd,yyyy");
As descripted in the documentation of SimpleDateFormat:
E Day name in week
d Day in month
y Year
You use SimpleDateFormat
DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy");
The format above is:
MMMM Month in year,
dd Day in month,
yyyy Year,
Please read the manual for more possibilities of how to format dates properly.
SimpleDateFormat format=new SimpleDateFormat("MMMM dd, yyyy");

Categories

Resources