how do I change date format of the following date
Thu May 17 00:00:00 GMT+05:30 2012
to
2012-05-17 00:00:00
I need it as date and not as string. I am using
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("")
But its not giving the result.
Actually I am storing the values date wise. So when someone enters the data for the same date again it should overwrite.
If I pass it as date object into the hibernate query it gives the result. But not always. ON few occasions it inserts again that is it inserts duplicate data for the same date. Befroe entering I am adding a method to check if data exists for that date. criteria.add(Restrictions.eq("date", date));
You shouldn't be doing string manipulation at all here. You've said in comments that it's a date/time field in the database, so why would there be any string conversion involved in your code?
Specify parameters in JDBC as java.sql.Date, java.sql.Timestamp or whatever - and then fetch them that way too. Don't do a string conversion. Ignore whatever format happens to be displayed when you query the database in a tool - don't think of the result as having a "format" at all - they're just dates.
String str ="Thu May 17 00:00:00 GMT+05:30 2012";
DateFormat old_format = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
Date newDate = null;
try {
newDate = old_format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat new_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = new_format.format(newDate);
System.out.println("==>"+date);
Do exactly as you do now but change parse("") to format(theDate)
theDate being the Date object you want to format
See
http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html#format(java.util.Date)
String date = "Thu May 17 00:00:00 GMT+05:30 2012";
String oldFormat = "EEE MMM dd hh:mm:ss z yyyy";
String newFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
sdf2.format(sdf1.parse(date))
Related
Im working on an RSS reader software. I get items with their pubDate (publish date) values as string, convert them to Date object, and put them to my DB. However, when I check my DB, I saw some interesting values such as the date of tomorrow.
I research this situation and found that it is about time zone value Z. For example when I get "Mon, 26 May 2014 21:24:29 -0500", it becomes "2014-05-27 05:24:29", the next day !
All I want is to get dates in any timezone and convert them to date in common timezone, such as my country's.
Here is my code :
public static String convert(String datestr) throws ParseException {
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse(datestr);
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return resultFormatter.format(date);
}
And I use the method like that :
System.out.println(convert("Mon, 26 May 2014 21:24:29 -0500"));
The output is : 2014-05-27 05:24:29
Any idea ?
Since you haven't set a time zone, it's using your system's default.
Set a specific IANA time zone.
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
resultFormatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
return resultFormatter.format(date);
Looks like you passed a Date with timezone, but given a wrong format. If you are passing timezone like "-0500" you should rather use:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Remember that the system will always display the date using the current, default timezone (TimeZone.getDefault()) unless you override it by:
resultFormatter.setTimeZone(...)
This is working as expected. The date is converted as per your system's timezone.
Check the UTC offset of your system and replace it in the sample date string and look at the output.
For e.g: India is UTC+5:30
String datestr="Mon, 26 May 2014 21:24:29 +0530";
output:
2014-05-26 21:24:29
Alternate solution
If you don't want to consider the timezone of the input date string then simply truncate this information and remove zzz from pattern as well as shown in below code:
String datestr = "Mon, 26 May 2014 21:24:29 -0530";
datestr = datestr.replaceAll("\\s[-+](\\d+)$", ""); // truncate the timezone info if not needed
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); // remove zzz from the pattern
Date date = formatter.parse(datestr);
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(resultFormatter.format(date));
I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd
Currently using Parse to obtain a date on an object by using:
Date date = object.getCreatedAt();
The returned String when displaying it in a TextView is this:
Mon Mar 17 22:39:27 CET 2014
However I really only want the MM/DD/YYYY to display like so: 3/17/2014
I've tried this:
Date date = object.getCreatedAt();
SimpleDateFormat originalFormat = new SimpleDateFormat("MMM DDD yyyy");
try {
Date originaldate = originalFormat.parse(date.toString());
finalDate = originaldate.toString();
} catch (java.text.ParseException e1) {
e1.printStackTrace();
}
but keep getting a ParseException for "Unparseable date", any idea what's going on? If I were to simply change this line back to this:
SimpleDateFormat originalFormat = new SimpleDateFormat("EEE MMM DDD HH:mm:ss z yyyy");
Then it prints out the full date again just fine with no parse exception, including all the date stuff I don't want.
Don't use parse method, use format instead :
Date date = object.getCreatedAt();
SimpleDateFormat formater = new SimpleDateFormat("M/d/yyyy");
String datestring = formater.format(date); // value is : 3/17/2014
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString()
java.util.Date.toString() method always returns a String of format
dow mon dd hh:mm:ss zzz yyyy
For example, Thu Jan 10 02:00:00 EET 1992.
Your Date format "MMM DDD yyyy" expects a date String like Jan 10 1992 where 10 represents not 10th day of January but 10th day of year 1992.
Therefore to convert a date to String and convert it back to Date object using your format, you need to do
Date originaldate = originalFormat.parse(originalFormat.parse(date.toString()));
Or to convert Date.toString() to Date object,
SimpleDateFormat toStringFormat = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date originaldate = toStringFormat.parse(date.toString());
Lastly, if you want a Date string with format like 3/17/2014, the correct format is M/d/yyyy.
Refer to http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for help on how to write Date format.
Given the following:
String dt = "Wed Jan 1 12:34:03 2010";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");
Date output = sdf.parse(dt);
Produces:
Wed Jan 1 12:34:03 ADT 2010
Where is the timezone coming from? I don't have z in my format pattern.
Thanks,
Doug
You're apparently displaying the toString() outcome of the Date object like as
System.out.println(output);
The format is specified in the javadoc and it indeed includes the timezone.
toString
public String toString()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
You need SimpleDateFormat#format() to convert the obtained Date object to a String in the desired format before representing it. For example,
String s = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(output);
System.out.println(s); // 01-01-2010 12:34:03
When a Date object is created, the timezone is set from the system settings.
try output.getTimeZoneoffset() and it returns the offset in minutes (for ADT it's -180)
Can any one help in conversion of date format?
My returned date object is contains "Mon Jul 12 00:00:00 IST 2010"
I'm trying to convert this date format to "MM/dd/yyyy" but I'm getting parse exception. Please help me how to convert it
Code from the OP's comment:
String mydatObj = myDate.toString();
Date formatedDate = getDateFormat(mydatObj);
public static Date getDateFormat(String dateString) {
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try {
// set isLenient to false to adhere to the date format.
format.setLenient(false);
date = format.parse(dateString);
} catch (ParseException parseException) {
// ignore
LOG.error(parseException.getMessage(), parseException);
}
return date;
}
Well, you are getting a ParseException since you are trying to parse a date with the wrong format.
Here is a small code snippet which will work with the format you have:
// parse the date
DateFormat f = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date d = f.parse("Mon Jul 12 00:00:00 IST 2010"); // works
// now print the date
DateFormat out = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(out.format(d));