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.
Related
I have an issue where I would like to get the start of the day, however it seems to be setting it to 12:00 via automatic.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Why is this spitting out:
2014-06-06 12:00:00
The 12:00:00 is the issue
That is because hh represents the hour in 12 hour format. You need to use HH instead.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Have a look at the docs for more info.
Also, on a side note, there is a typo in your code.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date todayDate = dfFull.parse("2014-06-06 00:00:00"); // todayDate must be of type Date and not String
String today = dfFull.format(todayDate); // today should be of type String
System.out.println(today);
You should use HH for hours in this case.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Now you will get the out put as
2014-06-06 00:00:00
And again if you use hh that mean you are using 12 hour format while HH means 24 hour format
So
I have a question, how can I convert a string like 20130706123020 to a date object.
So I what to convert the string 20130706123020 to a date object looking like:
2013-07-06 12:30:20
Attempted code:
String date = "20130706234310";
Date date1 = new SimpleDateFormat("yyyy-m-d H:m:s").parse(date);
System.out.println(date1);
Any suggestions will be appreciated.
Thank you!
You have to first parse the String using the parse method from SimpleDateFormat.
Then pass the Date object returned by the parse method to another SimpleDateFormat and then using the format method get the date in the format you want.
String s = "201307061230202";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS"); // format in which you get the String
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // format in which you want the date to be in
System.out.println(sdf1.format(sdf.parse(s)));
The significance of HH, hh, KK and kk in the hour field is different. I have used HH you can use the one according to your requirement.
H Hour in day (0-23)
k Hour in day (1-24)
K Hour in am/pm (0-11)
h Hour in am/pm (1-12)
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = sdf.parse("20130706123020");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf2.format(date));
use this :
long int my_date = 20130706123020L
and after that :
String date_Text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(my_date));
In my application I need to compare two different dates given in below formats.
Inputs:
there are 2 input dates in String format.
String actual="11/12/2012 11:26:04 AM";
String expected="21/12/2012 09:49:12 AM";
I am trying to use below java code for comparision.
SimpleDateFormat format= new SimpleDateFormat("dd/MM/YYYY hh:mm:ss a");
Date date1 = format.parse(actual);
System.out.println("Formated date1 is: "+format.format(date1).toString());
// prints : 01/01/2012 11:26:04 AM Why????
Date date2= format.parse(expected);
System.out.println("Formated date2 is: "+format.format(date2).toString());
// prints : 01/01/2012 09:49:12 AM Why????
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if( !(cal1.compareTo(cal2)<=0))
{
result=false;
String errMsg +="Actual:"+actual+" date is not before or equal to expected:"+expected+" date\n";
System.out.println(errMsg);
}
But the above code is not working as expected. please check the wrong output mentioned in comments
I think there is something wrong with the formatting.
can anyone please help me.
your format should be :
SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Notice year in lowercase y
Try:
SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
The Ys should be lowercase.
You can use Joda Time. It has really nice methods, like isBefore()
String actual = "11/12/2012 11:26:04 AM";
String expected = "21/12/2012 09:49:12 AM";
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/YYYY hh:mm:ss a");
DateTime dateTime1 = fmt.parseDateTime(actual);
DateTime dateTime2 = fmt.parseDateTime(expected);
if (dateTime1.isBefore(dateTime2)) {
System.out.println("awesome");
}
I am using jdk- 1.6.
I am try to parse String "24-10-2012" date to Date (24-10-2012) but i am getting this error:
java.text.ParseException: Unparseable date: "18-11-2012"
java.text.DateFormat.parse(DateFormat.java:354)
I am parsing like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
System.out.println(formatter.parse(currentDate));
prints
Wed Oct 24 00:00:00 CEST 2012
Your problem cannot be reproduced with the code you have posted.
My hypothesis: your exception is thrown from a piece of code other than the one you are accusing of the error. You could try carefully analyzing the stack trace in order to track down the real culprit.
Date in java does not hold any format. Read more...
When I run
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(date);
System.out.println(formatter.format(date));
I get
Wed Oct 24 00:00:00 BST 2012
24-10-2012
which is as I expected. Can you clarity what the problem is?
You can use this for the format "dd-mm-yyyy"
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(formatter.format(date));
import java.util.Date;
import java.text.SimpleDateFormat;
public class SimpleFormatDate
{
public static void main(String args[]){
Date todaysDate = new java.util.Date();
// Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
}
}
output
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
Wait a second.. Why u need to parsing that if u have a right value ?
Anyway, i use this :
SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd");
String sDate = oFormat.format("24-10-2012");
it will appearing date like 2012-10-24. So if u want to parsing to dd-MM-yyyy, u just need change the format to what u want.
NB : Sorry if my english is bad. :D
I am having problem with this small piece of code
SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String str = "2010-03-13 01:01:22";
Date date = sf.parse(str);
SimpleDateFormat f = new SimpleDateFormat("d MMM yyyy hh:mm aaa");
System.out.println(" Date " + f.format(date));
Output :
Date 13 Jan 2010 01:01 AM
The code seems to be fine but still I am getting month name wrong.
Please help !!
Thanks.
You are using minute instead of month in your pattern. It should be:
yyyy-MM-dd HH:mm:ss
mm stands for minute. You should use MM when parsing month:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");