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");
Related
This question already has answers here:
Changing String date format
(4 answers)
Closed 4 years ago.
I'm trying to convert a string to date but every time i do it keeps throwing errors at me, Im not sure what i am missing
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = format.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(date.toString());
My variable m.getEventTime() is passing the following string 2018-04-28 14:00:00
I have tried passing the string as 2018-04-28T14:00:00Z to no avail.
No errors are coming from the stack trace from the try/catch block but the log is printing out D/Response.Error.Date: 2017-08-19 15:00:00
when i add e.toString() to the log it prints out D/Response.Error.Date: 2017-08-19 15:00:00 java.text.ParseException: Unparseable date: "2017-08-19 15:00:00"
On the actual application when run the time is shown as now Sat Apr 28 08:22:33 GMT+01:00 2018
Am i missing something?
You can try this,
Date date = new Date();
SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = oldformat.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(format.format(date));
You can get time format like this:
String dateTime = null;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy, hh:mm a");
//here you can use your required format
dateTime = df.format(new Date(stringTime));
You are converting date into English which is causing the exception:
Please Change:
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
eventTime.setText(date.toString());
with
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a");
eventTime.setText(format.format(date));
How to convert string to date in us time zone format in java .I am trying to convert string to date in us time zone format but it is always taking IST format
TimeZone tz = TimeZone.getTimeZone("US/Alaska");
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
String date="Sun, 9 Mar 2014 02:00:00 EST";
Date d = null;
try {
d = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("date is"+d);
boolean inDs = tz.inDaylightTime(d);
System.out.println("inDs"+inDs);
while printing date Mar 09 12:30:00 IST 2014
Use Calendar instead of Date. Anyways the below solution of converting a date to Calendar and back to Date is not efficient. If you can get the day, month, year etc. seperately, its better to use cal.set()
TimeZone tz = TimeZone.getTimeZone("US/Alaska");
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
String date = "Sun, 9 Mar 2014 02:00:00 EST";
Date d = null;
try {
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(date));
cal.setTimeZone(tz);
System.out.println("date is " + cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am trying to format a date by parsing it and then formating it but it is not working.
It is showing a parsing exception
public java.util.Date convertFormat(String DateTimeForm)
throws ParseException {
DateTimeForm="2012-06-01 10:00 PM";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
java.util.Date FCDate = (java.util.Date) formatter.parse(DateTimeForm);
return (java.util.Date) FCDate;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
try {
Date date = formatter.parse("2012-06-01 10:00 PM");
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Didn't change anything and yet it works.
Fri Jun 01 22:00:00 CDT 2012
This works fine on my machine. I didn't change anything important.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
Date date = null;
try {
date = formatter.parse("2012-06-01 10:00 PM");
} catch (ParseException ex) {
// Intentionally empty. Failed parse causes date == null.
}
System.out.print(date);
prints
Fri Jun 01 22:00:00 EDT 2012
The Java docs say the numerics are all locale-independent, but not the AM/PM. For example the code fails if you specify Locale.JAPAN in the formatter construction. Specify Local.US to guarantee AM/PM will always work.
I am trying to add a new pattern to the date display but I am not getting the result that I am expecting:
Here is my code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
sdf.applyPattern("yyyy-MM-dd");
Date date_out = null;
try {
date_out = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((TextView) view.findViewById(R.id.date)).setText(date_out.toString());
I want the output to look something like this: 03 Oct 2011
However this is the output tat I am getting: Oct 03 00:00:00 GMT+ 11:00 2011
How do I reach the desired output?
EDIT:
I solved this code by adding this line:
sdf.format(date_out);
instead of setText()
Date.toString(); does always format your String that way. You should a SimpleDateFormat to format the Date object to the String you want.
The JavaDoc of the Date.toString(); method says:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
You have to use two SimpleDateFormat objects. One for parsing the input and an other one for formatting the parsed Date object to String.
final String inputDate = "2011-05-08T11:12:13.0123";
final SimpleDateFormat inputParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date date_out = null;
try {
date_out = inputParser.parse(inputDate);
} catch (final ParseException e) {
e.printStackTrace();
}
final SimpleDateFormat outputFormatter =
new SimpleDateFormat("dd MMM yyyy", Locale.US);
final String result = outputFormatter.format(date_out);
System.out.println(result);
Output:
08 May 2011
The Date.toString() method formats the string like that (check the api documentation).
You do not actually need to use applyFormat(...) in this case. You want to parse one format and output it in another format.
To parse the date (given the string: 2011-10-03") use can use the format"yyyy-MM-dd"and when you output theDateyou want to use"dd MMM yyyy"`:
public static void main(String[] args) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy MMM dd");
Date parsedDate = inputFormat.parse("2011-10-05");
System.out.println(outputFormat.format(parsedDate));
}
Outputs (on US locale):
2011 Oct 05
Read below document :-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
hope help u above link.
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.