I have a string date something like below , I want to convert it in to java.util.Date.
String fromDate = "03/19/2009";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date dtt = df.parse(fromDate);
System.out.println("the date is "+dtt);
But I am getting out put as Thu Mar 19 00:00:00 IST 2009, but I need it as 03/19/2009.
Please help me out.
You can use the same DateFormat and its SimpleDateFromat.format() to get the desired output.
String fromDate = "03/19/2009";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date dtt = df.parse(fromDate);
System.out.println("the date is " + df.format(dtt));
You need to parse it again to the format you desire.
What you are currently printing is the value returned by the Data objects toString method.
You code works well. dtt actually holds the correct Date value.
When you print a Date, it uses toString().
If you want to print it in a specific format, you need to use another SimpleDateFormat and call format method.
Call method format on the SimpleDateFormat instance df.
try this
String DateStr="03/19/2009";
SimpleDateFormat sim=new SimpleDateFormat("MM/dd/yyyy");
Date d = new SimpleDateFormat("MM/dd/yyyy").parse(DateStr);
System.out.println(sim.format(d));
output 03/19/2009
You are printing a date not converted to string, so your result is correct, but if you want to print it as String, you shall reformat it as:
System.out.println("the date is " + df.format(dtt));
Related
I have a string like this 210115 I want to represent it as 21:01:15 any ideas?.
I tried using Gregorian calendar but it adds date to it which I don't want
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
Date date = new Date();
try{
date = sdf.parse("210115");
}
catch(Exception e){
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
System.out.print(calendar.getTime());
Output is Thu Jan 01 21:01:15 UTC 1970 but what I want is just 21:01:15
Thanks.
To output a formatted date, you use another SimpleDateFormat object with a pattern with the format you want.
In this case, it sounds like you might want to use something like
SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println( outputFormat.format(date) );
So what you want is just a time, without time zone. I would recommend using the LocalTime class, which is exactly that, instead of the Date class.
LocalTime time = LocalTime.parse("210115", DateTimeFormatter.ofPattern("HHmmss"));
If u r getting the date string in "210115" this format and you want it in "21:01:15" format then why are you using date format.
Simply do string operation as:
String time="210115";
String newtime=time.substring(0,2)+":"+time.substring(2,4)+":"+time.substring(4,6);
System.out.println(newtime);
you will get the required format.21:01:15
I am trying to print two dates using SimpleDateFormat but for my custom date the output looks completely different.
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date2 =dateFormat.parse("01/01/2014 10:45:01");
System.out.println(("date2:"+date2));
Date date = new Date();
System.out.println(dateFormat.format(date)); // how it prints this is the desired outcome
OUTPUT:
date2:Wed Jan 01 10:45:01 GMT 2014
11/04/2014 10:45:50
The output is correct. You are creating date2 by parsing the date in string with the DateFormat. But when you print date2, you are not printing it with dateFormat.format() and hence the date is printed in its default format.
Try System.out.println("date2:"+dateFormat.format(date2));
format() will return you string of date in your desired format.
Date Object ---------->SDF Fomatter------>Formatted date in String
parse() accept the date in string format (your customize format) and return you Date Object
Formatted String date ------>SDF parse----->Date object
Wanna check, print it's value:
dateFormat.format(dateFormat.parse("01/01/2014 10:45:01"));
You have parsed it using dateformat, But you need to format it to get desired output.
System.out.println(("date2:"+dateFormat.format(date2)));
Did you try dateFormat.parse("dd/MM/yyyy HH:mm:ss"); ?
The line
System.out.println(("date2:"+date2));
Implicitly calls the toString() method on the date2 parameter. As Date has overridden the toString() method it inherits from Object, it is that method that dictates the format of the output. The Javadoc for Date#toString() states:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
Which matches what you're seeing in your output. In order to get the output that you were expecting, you would need to do the following instead:
System.out.println(("date2:" + dateFormat.format(date2)));
Date objects don't have a format associated with them. They're a dumb object that doesn't need to know about any display formatting details, as they aren't relevant to the date itself.
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
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 have My Database data in this format
18-NOV-10
I have to pass the same format into java.util.Date like this
Date date = new java.util.Date(dateformater);
so that the result of java.util.Date is like this 18-NOV-10
Is this possible ??
I tried this way
String strDate = "12-NOV-07";
SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MMM-yy");
Date date = sdfSource.parse(strDate);
System.out.println(date);
But i am getting the result as "Mon Nov 12 00:00:00 IST 2007 " which i want it only
12-NOV-07"
You can use java.text.DateFormat (actually SimpleDateFormat) to get you where you want to go, but maybe you shouldn't be storing the dates as strings in your database. It will do output and parsing.
SimpleDateFormat sdf =
new SimpleDateFormat("DD-MMM-YY");
Date parsed = sdf.parse(dateString);
See http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/
Once you get the Date, you can turn it into the format you want but it will be held in memory as a Date object. You can get it in the form you want using
String dateString = sdf.format(parsed);
As others have pointed out, you should probably store your dates as dates, not strings; nevertheless...
If you want to turn a Date back into a string in that format you can use the following:
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = new Date();
String dateStr = formatter.format(date); // Gives "22-May-11"
If you need MAY instead of May, just use toUpperCase() on the resultant string.
DateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
Date d = sdf.parse("18-NOV-10");
Try System.out.println(sdfSource.format(date).toUpperCase()); instead. The Date object will always have a time component to it; there is no way to "disable" that feature. What you can do instead is to ignore it in your calculations and display. If all Date objects you use are set to the same time of the day, then you can safely ignore the effect of the time component in your comparisons. If you look carefully, the time component of your Date object is set to midnight.