Parsing date gives month of 00 [duplicate] - java

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.

Related

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

Java - store current date WITHOUT TIME into a Text file [duplicate]

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I really need your help. I have searched and tried every example I could use, but none have worked.
I need to store current date in YYYY-MM-DD format in a text file..the String date has to be a string..
String dateF = "YYYY-MM-DD";
Date dateOnly = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateF);
String date = dateFormat.format(dateOnly);
when I tried that code above.. this the output I got
please|work|2016-04-110|11
please help me...this is my assignment due this Friday ): I just need this date and 2 other things to be done..
thanks :)
your issue comes from the case you used for Y and D. according to the API SimpleDateFormat documentation, you should use d (day in month) instead of D (day in year), in your format definition.
String dateF = "yyyy-MM-dd";
Format being used is incorrect.
YYYY-MM-DD : Capital DD will return Day in the year. So, 11 April corresponds to 110th day in the year.
yyyy-MM-dd : Small dd will return the Day in the month.
Refer: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this code :
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(c.getTime());
The formattedDate contain 2016-04-19
Edit :
In your code change the YYYY to yyyy and DD to dd.

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

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!

Convert String to Date [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
From this question and its answers, I tried to convert string to date. But it seems to strange with me.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
It returns
Mon Dec 29 11:56:00 ICT 2014
I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?
First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);
The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.
Try this:
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));

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

Categories

Resources