I have a TimeStamp '2013-06-24 10:46:11.0' and I need to cut off the .0 part, so what I did was to use the SimpleDateFormat to parse it to String and back then parse it to date, the first conversion was fine but the second (string to date) throws a java date time.
public void convert(Object object) {
Date date;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = object().getDate();
String formated = format.format(date);
try {
date = format.parse(formated);
} catch (ParseException ex) {
Logger.getLogger(DlgConsultaFactura.class.getName()).log(Level.SEVERE, null, ex);
}
}
What I expect is a date like this 2013-06-24 10:46:11, but what I got is this date Mon Jun 24 10:46:11 CDT 2013
Any help will be appreciated. Thanks.
Mon Jun 24 10:46:11 CDT 2013 and 2013-06-24 10:46:11 is actually same value. Mon Jun 24 10:46:11 CDT 2013 is as per your default locale.
You're getting confused between date's internal representation and its display format.
To print in 2013-06-24 10:46:11 you can use same SimpleDateFormat object again.
You can use DateFormat#format(Date) to print the date or return the String representation in your desired format i.e. "yyyy-MM-dd HH:mm:ss". Something like this:
String myDt = format.format(date);
// 2013-06-24 10:46:11
Not the best way but a quick and easy one if you want just the string representation of the date ...
formated = formated.substring(0, formated.length()-2);
:)
DateFormat i.e. SimpleDateFormat just formats the date as per your need.
Parse returns the Date representation of the String (or timestamp in your case) passed in.
Format returns the String representation of the Date object passed in.
In both the cases , you see the same date just the representation is different.
Related
I try to convert my date which is in this format:
Wed Sep 18 00:00:00 IST 2013 to "18-09-2013 00:00:00" .
I use
SimpleDateFormat sdf = new SimpleDateFormat(dd-MM-YYYY hh:mm:ss);
String abc = sdf.format(myDate);// which gives me expected Date format as "18-09-2013 00:00:00"
But when i try to convert String to Date again using :
Date newDate = sdf.parse(abc); // gives me the default date format as Wed Sep 18 00:00:00 IST 2013
What can i do to get result as 18-09-2013 00:00:00 in Date return type
A date as such has no format, but you can use a formatter to generate a formatted version of the date or to parse a string to a date (as you already did). What you can do is to subclass the Date class and to override the toString() method. This way you will see the expected format in case you debug code in your IDE, log the date etc.
class MyDate extends Date{
#Override
public String toString(){
return sdf.format(this)
}
}
Please help me with the following code. I got unparseable date exception.
public static Date getUtilDateFromString(String date) throws ParseException {
return getUtilDateFromString(date, null);
}
public static Date getUtilDateFromString(String date, String format)
throws ParseException {
if (format == null || "".equals(format)) {
format = "yyyy/MM/dd HH:mm:ss";
}
DateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(date);
}
The Date format should be yyyy-MM-dd HH:mm:ss.S since you're dealing with milliseconds. Your date pattern has mistakes too.
This works:
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2014-11-26 12:22:44.0"));
Output:
Wed Nov 26 12:22:44 CAT 2014
I would check the String date variable you are using, it seems that it does not fit the format you are trying to implement.
For example if you call getUtilDateFromString("2014-12-06 12:26:11") it will not work because the SimpleDateFormat will be expecting a String date with the format "yyyy/MM/dd HH:mm:ss".
If you call getUtilDateFromString("2014/12/06 12:26:11") will work perfectly fine because the
String date fits the format "yyyy/MM/dd HH:mm:ss".
Hope it helps.
my problem is the following. I would like to have the german date for the string "11.11.2012".
I tried this piece of code:
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.mm.yyyy",Locale.GERMANY);
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
But it gives me the wrong format. "Wed Jan 11 00:11:00 CET 2012", instead of "11.11.2012".
Thank you, any help is appreciated.
You have to use M in you pattern, because M is the month and m is the minute!
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
^^^^
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
For more information see the documentation of SimpleDateFormat
but i still have the same output: Sun Nov 11 00:00:00 CET 2012
Try to understand the thing when you use
SimpleDateDFormat#parse() - It parses text from a string to produce a Date.
and Date object in java always contains date along the time.
Javadoc says Date() - Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
and FYI Sun Nov 11 00:00:00 CET 2012 is equal to 11.11.2012
Edit: try this
public static Date convertUtilDateToSqlDate(java.util.Date date){
if(date != null && !(date.equals(""))) {
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
return sqlDate;
}
return null;
}
pass the util date you got above and this method shall return the sql format date then store the sqlformat date in mysql column-field of type Date
String selectedDate = "2012-" + createdMonth + "-" + createdDay;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
createdDate = dateFormat.parse(selectedDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
System.out.println(createdDate);
Basically when I print createdDate, it will display something like this :
Thu Mar 08 00:00:00 CST 2012
Instead of something of this format "yyyy-MM-dd". Thanks a bunch!
The parse method returns a java.util.Date and that is the what the Date implementation of toString() returns.
You need to print as below. Point is that you need to use the formatter object you have created while printing as well.
System.out.println(dateFormat.format(createdDate));
use dateFormat.format(createdDate)
You seem to think that createdDate, which is a Date object, has the format yyyy-MM-dd. It doesn't. Date objects don't have a format - they just contain a timestamp, just like numbers are just numbers, which don't have a format by themselves.
A SimpleDateFormat object is used to parse a String into a Date object, or format a Date object into a String.
If you have a Date object and you want to display the date in a particular format, then convert it to a String with the appropriate format using a SimpleDateFormat object:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String text = fmt.format(createdDate);
System.out.println("Created: " + text);
If you print a Date object without explicitly formatting it, it will be formatted using a default format, which is why you see Thu Mar 08 00:00:00 CST 2012.
A Date object does not somehow remember what the format was of the String that you parsed it from.
When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.
I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.
String timestr = "12:12";
Date convertedDate = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Thanks!
I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.