Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.
Related
I want to change date format which I received reading from excel cell file is "30-mar-2016" to 03/30/2016. I have tried below
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate;
startDate = df.parse(inputDate);
It gave me- java.text.ParseException: Unparseable date: "30-mar-2016"
.
I also tried below code
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
String startDate;
startDate = df.format(inputDate);
getting -- java.lang.IllegalArgumentException: Cannot format given Object as a Date
Can anyone help me .
Your input string 30-mar-2016 is in the format dd-MMM-yyyy. Your output format is MM/dd/yyyy. So you need two DateForamts. One for parsing original input string, one for formatting output string.
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy"); // for parsing input
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy"); // for formatting output
String inputDate = "30-mar-2016";
Date d = df1.parse(inputDate);
String outputDate = df2.format(d); // => "03/30/2016"
I have resolved using below
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse("30-mar-2016");
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));//got 03/30/2016
Thanks everyone for your quick response
When I convert the String "07/02/2014" (mm/dd/yyy) to a java.util.Date using SimpleDateFormatter I get a result of Sun Dec 29 00:00:00 CAT 2013.
Here is the code I am running:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/DD/YYYY");
Date exactDate = formatter.parse("07/02/2014");
Why is this happening ?
It must be:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
The documentation explains why.
y Year
Y Week year
D Day in year
d Day in month
M Month in year
m Minute in hour
Try this one,
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
When you print the Date object you will get that output. Try formatting Date into String for desired format and output.
Try following code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
String date=new SimpleDateFormat("MM/dd/yyyy").format(exactDate);
Hope this helps.
Can anyone please clear me, what does this exception mean:
Exception in thread "main" java.text.ParseException: Unparseable date: "06.10.2013 00:00".
That was found such a date in file 06.10.2013 that cannot be parsed? must it be always 06/10/2013? I got it, when i convert String[] to Timestamp while reading file. But I have two formats in a file like 03/10/2013 and 03.10.2013, which must I use then, instead of this:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
String[] temp = line.split(",");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy HH:mm" );
java.util.Date parsedDate = dateFormat.parse(temp[0]);
java.sql.Timestamp timestamp = new java.sql.Timestamp(
parsedDate.getTime());
for (int i = 1; i < temp.length; i++) {
o.setTimestamp(timestamp);
i have default both of formats in my file 06.10.2013 15:00:00 and 06/10/2013 15:00:00
Perform an analysis on the String before naively try to parse it with your current SimpleDateFormat:
SimpleDateFormat dateFormat;
//check if the string to parse contains a dot
if (stringContainingTimestamp.contains(".")) {
dateFormat = new SimpleDateFormat("MM.dd.yyyy HH:mm");
} else {
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
}
//rest of the code...
Another option may be just replacing the dot char by slash using String#replace(char, char):
stringContainingTimestamp = stringContainingTimestamp.replace('.', '/');
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
//rest of the code...
The choice is yours.
I'm trying to set a date format, but when i run this code
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
System.out.println("datefield = "+date);
i take result:
oldstring = 2013-01-1
datefield = Tue Jan 01 00:01:00 MSK 2013
Why datefield isn't equal 2013-01-1?
At first mm in yyyy-mm-dd mean minute not Month. to set month use MM.
It would be look like this :
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
UPDATE
Try this:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
String sdf = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.out.println("datefield = "+sdf);
If you don't use new SimpleDateFormat("yyyy-mm-dd").format(date);
you getting standard date format which include all info. If you want special format you need to use
new SimpleDateFormat("yyyy-mm-dd").format(date);
Also read this article about date formatting
The type of datefield is Date, so the toString method will basically always return the same format, as you are not overriding it.
So what you need to do, is basically:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(oldstring);
System.out.println("datefield = "+date);
String outDateStr = sdf.format(date);
System.out.println("newstring = "+outDateStr);
Use MM for month. mm is for minutes
I would like to get the date in numbers in Java.
For example: today is 13th of dec, 2012. I would like to get it in numbers as 13.12.2012
How can I achieve it? Any help would be really appreciated.
P.S I tried something like:
Date d = new Date();
String date = d.toString().substring(0, 10);
But got the output as:Thu Dec 13
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
String formattedDate = df.format(new Date());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String res = format(d);
You can use SimpleDateFormat to achieve that
new java.text.SimpleDateFormat("dd.MM.yyyy").format(new Date());