I have a list with many Date Strings such as "Fri, 08 Apr 2011 22:28:00 -0400" and need to parse them to a proper format (Friday, 08. April 2011).
My problem is that the device needs a very very long time parsing > 10 date objects and occasionally runs out of memory. Is there a more efficient way parsing dates as:
SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
// later in code
try {
Date date = sdf.parse(myDateString);
return DateFormat.format("dd. MMMM yyyy", date).toString();
} catch (java.text.ParseException e) {
}
How can I parse many date Strings very fast?
Try this : Alternatives to FastDateFormat for efficient date parsing?
One thing you can do is to store the output DateFormat, like you do with the input DateFormat, so that you don't have to create a new one every time:
DateFormat parser = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
DateFormat formatter = new SimpleDateFormat("dd. MMMM yyyy");
// later in code
try {
Date date = parser.parse(myDateString);
return formatter.format(date);
} catch (java.text.ParseException e) {}
If possible, assign this task to your database (assuming SQLite) and see if its Date and Time function are useful in your case.
Related
I have two dates.
The first date is the system time. The second date is related to a news article and when the article expires, it is called end_time.
Im using selenium to test that the article does in fact expire when the system time exceed the the end_time.
My code is as follows:
String searchstring = poriginal;
//make objects to be compared
Date parsed_system_time=null;
Date parsed_end_time=null;
//generate a current time object
GenerateSimpleTime current_time = new GenerateSimpleTime();
current_time.setSystem_time_snapshot();
String system_time = current_time.getSystem_time_snapshot();
//set up the SimpleDateFormat to be used for parsing the strings into objects for comparison
//parsing the date format e.g : 04:11:2016 11:34 AM
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy hh:mm");
try{
System.out.println("Trying to parse system time: \n");
parsed_system_time = sdf.parse(system_time);
}
catch(ParseException e)
{
System.out.println("Couldnt parse system time...\n");
e.printStackTrace();
}
SimpleDateFormat end_time_sdf = new SimpleDateFormat("dd MMMM, yyyy hh:mm a");
try {
parsed_end_time = end_time_sdf.parse(end_date);
} catch (ParseException e) {
System.out.println("Couldnt parse end_date...\n");
e.printStackTrace();
}
while(parsed_system_time.before(parsed_end_time))
{
current_time.setSystem_time_snapshot();
try {
system_time = current_time.getSystem_time_snapshot();
parsed_system_time = sdf.parse(system_time);
System.out.println("endtime is: "+ parsed_end_time+"\n");
} catch (ParseException e) {
System.out.println("Couldnt parse current_time.getSystem_time_snapshot()...\n");
e.printStackTrace();
}
//System.out.println("system time is: \n");
}
When i run the program the dates are in the following format
endtime: Fri Nov 04 13:49:00 AEST 2016
systemtime: 04:11:2016 1:52 PM
if it a problem when comparing the two dates if they are in a different format. It shouldn't matter right?
When I run the test my program goes and runs indefinitely and doesnt detect when system time is greater than the end time.
The setSystem_time_snapshot() does the following:
String pattern= "dd:MM:YYY h:mm a";
SimpleDateFormat simpletime = new SimpleDateFormat(pattern);
system_time_snapshot = simpletime.format(new Date());
System.out.println("system time snapshop is "+system_time_snapshot+"\n");
Any ideas where I clean up this mess and get it working properly?
So your setSystem_time_snapshot() is returning a string in the format of
dd:MM:YYY h:mm a
But your sdf is
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy hh:mm");
If you endtime is: Fri Nov 04 13:49:00 AEST 2016,
you should use "EEE MMM dd HH:mm:ss zzzz yyyy" in your SimpleDateFormat
SimpleDateFormat end_time_sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
I am using shannah´s Data Access Library to access my objects via his DAO interfaces.
I face a very strange behaviour with parsing the date values when the unmap method gets called once my stuff is in the database. It only fails on CEST (Central European Summer Time)
I tried to use the NumberUtil.dateValue Method to parse it but it still fails...
java.lang.RuntimeException: Failed to parse string date format Thu Mar 31 00:00:00 CEST 2016. Could not find appropriate format parser.
i defined DateFormat´s as these
dateFormats[0] = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
dateFormats[1] = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
dateFormats[2] = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
dateFormats[3] = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
also tried to trim the String but it didnt help.
Im out of ideas as SDF doesnt have the constructor as the normal JDK with the 2nd parameter beeing the Locale.
// Method that take date time string , input pattern and out put pattern,that return formatted date as string
public String parseDate(String dateTime,String inputPattern, String outputPattern) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern, Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse(dateTime);
String str = outputFormat.format(date);
return str;
}
// call method
String date = parseDate("Thu Mar 31 00:00:00 CEST 2016" ,"EEE MMM dd HH:mm:ss zzz yyyy", "MM-dd-yyyy:HH:mm:ss");
// print date
System.out.println(date);
// Result
03-31-2016:03:00:00
I now did a workaround to this issue, i can still develop my api and stuff further but to be honest i dont like this fix, as i think it will make some problems with the TimeZones later.
Can someone with more experience tell me anything about this fix, if it may cause errors with TimesZones and Summer/Winter times?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
String dateValue = plannedDate;
String year = dateValue.substring(dateValue.length() - 4);
dateValue = dateValue.substring(0, 20);
dateValue += year;
this.plannedDate = sdf.parse(dateValue);
Any advice or possible fixes are welcome (y)
I have date strings in this form Thu Aug 02 00:00:00 GMT+00:00 2012
I have tried to use this method to parse these String in a Date object
public Date fromStringToDate(String data) {
Date result;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
try {
result = sdf.parse(data);
return result;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
But doesn't works and I get this error
java.text.ParseException: Unparseable date: "Thu Aug 02 00:00:00 GMT+00:00 2012"
I suppose that the problem is caused by a wrong SimpleDateFormat, but I don't know the right syntax to fix it.
You need to adjust the date format to the given string:
EEE MMM dd HH:mm:ss Z yyyy
Make sure use the correct placeholders, case sensitive, etc. Take a look to the Date and Time Patterns.
Sorry, I had a mistake with the 'z' pattern, 'Z' is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Take a look to Locale.US, it is important to apply because the months and and days are in english.
Use this date formatting:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
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))
I have a date in the format:Thu- Mar 22 2012.Its is obtained in a string variable date1.I need to convert the date in string variable to date format.I tried the below ccode but failed to parse the date;Please help.
DateFormat formatter;
Date formatted_date= null;
formatter = new SimpleDateFormat("EEE-MM d yyyy");
try {
formatted_date= (Date) formatter.parse(date1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You need three Ms 'MMM' to parse a months abbreviation like 'Mar'. And please check the whitespace after 'EEE-'.
Change it to following format
EEE- MMM d yyyy
Note space after - and MMM
Thu- Mar 22 2012
EEE- MMM dd yyyy
I think you need something like this
UPD: for date formatting:
SimpleDateFormat toStringFormatter = new SimpleDateFormat("EEE MMM dd yyyy");
String formattedDate = toStringFormatter.format(date);
so parse() is for String -> Date, and format() is for Date -> String
If the already posted answers doesn't work then you surely have a problem with the locale. Try the following SimpleDateFormat:
formatter = new SimpleDateFormat("EEE- MMM d yyyy", Locale.ENGLISH);
I thank With the given mode and given the locale's default date format symbols constructor SimpleDateFormat. Note: This constructor may not support all locales. To cover all the language environment
public SimpleDateFormat(String pattern,Locale locale)
//local:Locale.ENGLISH
//default is not English
formatter = new SimpleDateFormat("EEE, MMM d, ''yy", Locale.ENGLISH);
try {
formatted_date= (Date) formatter.parse("Wed, Jul 4, '01");
} catch (ParseException e) {
e.printStackTrace();
}