I'm using JComboCalendar widget to let the user select a date. JComboCalendar has a method getDate() which returns a Date object.
According to Oracle docs, Date.toString should return a string with the format yyyy-mm-dd, but instead I'm receiving strings like:
Fri Nov 08 13:08:38 CET 2013
How can I get the expected output? Bonus for how to format it like dd-mm-yyyy, since I just found how to read a String in a given format, not how to format the output.
Use the following to format your date:
new SimpleDateFormat("dd-MM-yyyy").format(comboCalendar.getDate())
As to your other question, you opened the wrong documentation.
getDate() returns a java.util.Date not a java.sql.Date
Correct documentation: http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString%28%29
try this
SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
Date d=new Date();
System.out.println(d1.format(d));
output 2013-11-08
SimpleDateFormat will help you.
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String str = sdf.format(yourDateInstance);
Try this out :
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatDate.format(new Date()); // Sample date to be formatted
System.out.println(formattedDate); // Watch the output
Related
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 6 years ago.
I have date in string object. I want to convert into Date object.
Date getDateFmString(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(dateString);
return convertedCurrentDate ;
}
above function returning following output.
Fri Apr 22 00:00:00 IST 2016
but I want output in this format '2016-03-01' only
function should take string only.
function should return Date object.
I have done lot of research over web, but I got solution from one Expert.
Date getDateFrmString(String dDate)
{
java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
return dDate;
}
this is what I want.
Change the date format from
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
to
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Hope this works
See this example
public Class DateFormatDemo{
public static void main (String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";
try{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch(ParseException e){
e.printStackTrace();
}
}
}
This link might help you with string to date object conversions
You are parsing with the wrong format try
String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Output:
2016-01-01
DEMO1
And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.
String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
OUTPUT:
01-01-2016
DEMO2
To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.
Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".
Let's do it step by step:
You have a date as String in dd-MM-yyyy format.
You want to convert it into date. (for this you are using SimpleDateFormat)
Now you are printing the date. Question here is are you printing the converted date object or input string?
If its a date object then toString method is called of date class.
As per comment on java.util.Date class it's:
dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016
So that coincides with what you get in output in the second approach. But how is that code even running is strange.
String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);
Variable 'input' is not defined.
What are the possible solutions:
While printing date, convert it back to String using SimpleDateFormat as per the requirement.
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dStr = sdf.format(dateString);
System.out.printn(dStr);
Extending class java.util.Date and override toString, but that would be a bad idea.
I want to convert String into Date in Java.
I have written following code for that:
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSSSSS");
Date date = dateformat.parse("2015-07-16 23:59:59.0");
dateformat.format(date);
After parsing I am getting following value for date : Fri Jan 16 23:59:59 IST 2015
I have tried many examples but didn't get proper solution.
Thanks in advance.
"mm" in the format string stands for minutes. You need to specify "MM" for the month part, or it defaults to January:
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
As you said you want to convert string to date then it will be received only in that format.
We cant format date in java we can format string representation of date using dateforamtter but if you are converting string to date it will be received in only that format that will be unformatted.
I have a string type date. I want to change it firstly into date datatype. But it is not formating date properly as I want.Here is my code
String a="12/11/2010";
Date d=new Date();
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yy");
d=as.parse(a);
System.out.println(a);
Output is like this :Sun Jan 01 00:00:00 IST 2012
But I want output like this 12-10-2014 in Date data type, not in String data type.
System.out.println(as.format(d));
You should use your dateformatter object to format your date d.
If you want to parse it as 12/11/2010 and output as 12-11-2010 you could have 2 different dateformatters one for parse and one for output. Ex.
String a="12/11/2010";
Date d=new Date();
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat as1=new SimpleDateFormat("dd-MM-yyyy");
d = as.parse(a);
System.out.println(as1.format(d));
A Date is an instant in time, and although it has a toString() that is not replaceable. Instead you use a DateFormat (like the one you have) to format the output
String a="12/11/2010";
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yy");
Date d=as.parse(a);
System.out.println(as.format(d));
How can i get data in format "YYYY-MM-DD 00:00:00.0" using class Date (it's extremly important to use exactly this class)?
I tried to do everything i can think of:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
df.format(date)
and
String pattern = "yyyy-MM-dd HH:mm:ss.S";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("2011-01-18 00:00:00.0");
} catch (ParseException e) {
e.printStackTrace();
}
byt when i print date using logger i get this format "Tue Sep 30 00:00:00 MSK 1913".
Try this
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = format.parse("2011-01-18 00:00:00.0");
System.out.println(format.format(date));
Are you sure you want the hours, minutes, secs to be zeroes?
Or do you mean the pattern yyyy-MM-dd HH:mm:ss ?
The date class is always independent of the formatting. It only needs to be translated when you print it, like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String out = df.format(date)
System.out.println(out);
Or do you want to strip the time out of the date object? or something.
You are confused by Date.toString() and SimpleDateFormat.format()
An object of Date (java.util.Date) has no format information. If you call date.toString(), (which is called by your logger), you got default representation of this object, you have seen what it is.
However, SimpleDateFormat.format() will give you a string as return value, this value will format the Date object with a pattern defined by SimpleDateFormat.
In your code, you first parsed the string, with certain pattern, to get the date object. If it was successful, you got the Date object, here, for this date object, you don't have any format information, even if you have defined a pattern to parse the input string. If you want to print/output (to string again) the date object, you have to use the SimpleDateFormat.format() method.
I hope the below one will solve your problem when you need to do for dynamic dates.
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String formattedDate = sdf.format(today);
System.out.println(formattedDate);
//Above line output (formatted String object) is 2017-12-29 00:00:00
System.out.println(format.format(formattedDate));
//Above line output(parsed Date object) is Fri Dec 29 00:00:00 IST 2017
For Date object you can't get the output as yyyy-MM-dd 00:00:00, but you will get this format in String object type.
I want to store today's date in the format yyyy-mm-dd. before storing I have took today's date,formatted it and again parse the formatted string to date. It gives the output date in a different format other than what I want. How can i get the date, format it in 'yyyy-mm-dd'
and again convert it into date and want the output in the format 'yyyy-mm-dd'.Please find the below code and tell me where I am wrong
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
java.util.Date date1;
String datestring=dateFormat.format(date);
try {
date1=dateFormat.parse(datestring);
System.out.print(date1);
} catch (ParseException ex) {
Logger.getLogger(accordcclass.class.getName()).log(Level.SEVERE, null, ex);
}
The output for the above code I get is
Thu Mar 07 00:00:00 GMT 2013. But I want the output as 2013-01-07
I had the same problem, this is what I did:
DateFormat inputDateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(outputDateFormat.format(inputDateFormat.parse("09-SEP-2013 10:00")));
That way I can parse the date in the original format and output it in the database compatible format.
There is the posibility of using PreparedStatement as someone mentiones before but I don't want to.
Don't use a Date object to print, use directly your datestring variable. Using a Date will call toString which will be formatted using the Locale.
Edit : Adding that if you want to store your Date variable with a format, it doesn't work that way. A Date doesn't hold a format, it just represents the time. How you show it in a GUI, console or anywhere else is where you need to specify a format if you want it to differ from the current Locale format.
You're using the DateFormat to format and reparse.
You don't need to re-parse. Simply use the DateFormat only to format.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String datestring=dateFormat.format(date);
System.out.println( datestring );