executing this piece of code:
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdfIn.parse(value11);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sdfOut = new SimpleDateFormat("MMM d, yyyy");
System.out.println(sdfOut.format( date ));
I an getting this output nov 23, 2005 instead of Nov 23, 2005 which would be much better.
Does anybody knows how to change it??
Thanks in advance
The exact strings that get generated depend on the locale you're in. If you just use
new SimpleDateFormat("MMM d, yyyy");
then the system default locale will be used. Your default locale probably renders the month as nov rather than Nov.
if you want a specific locale to be used, pass it in to the constructor, e.g.
new SimpleDateFormat("MMM d, yyyy", Locale.US);
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");
There is the following info:
date ("2013-08-30 07:05:25")
timezone ("Europe/Moscow")
My app gets this info from some server, and I need to convert this date/time to current user's timezone. I know that I should use DateFormat and TimeZone APIs, but I don't understand how. Please, give me a piece of advice or some code. Thanks.
Below code will work for you & refer this stack question for timezones-in-java :
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
cal.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date resultdate = new Date(cal.getTimeInMillis());
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
System.out.println("String date:"+sdf.format(resultdate));
try {
System.out.println("Date:"+sdf2.parse(sdf.format(resultdate)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OUTPUT
String date:2013-08-31 06:25:54 PM
Date:Sat Aug 31 18:25:54 IST 2013
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();
}
I´m having a stupid problem with java.util.Date.
I have this line of code, but I don´t understand why this date is unparseable with this format.
public class TestTime {
public static void main(String[] args) {
final String DATE_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";
String date = "Sat Dec 31 10:00:00 CET 2011";
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT);
Date lDate = null;
try {
lDate = dFormat.parse(date);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
If your system uses a locale other than English you need to use this constructor:
SimpleDateFormat(DATE_FORMAT,Locale.ENGLISH);
If this is not the problem, you should format a date using the same formatter and compare the output to your input string.
I don't see anything wrong with this. It executes for me without error, and returns:
Sat Dec 31 09:00:00 GMT 2011
Seems to be a Locale-related problem.
If I set a French locale, the pattern does not work. If I set the Locale to be US in the SimpleDateFormat constructor, it does works.
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
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.