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
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");
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.
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);
I am developing an application and I am stuck in converting string like 01/01/2037 01:00:00 AM
to Date
I used
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa")
Date d = dateFormat.parse(dateString);
but I get an error, any help will be appreciated.
you are converting this 01/01/2037 01:00:00 AM
therefore use
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa")
(more info in [documentation])1
then
Date date = dateFormat.parse("01/01/2037 01:00:00 AM");
keep in mind you have to wrap a try-catch around the parse method.
The problem is that the format you declared is nothing like the String you are trying to parse:
your String uses / to separate day, month, year while in your formatter you use -
your string separates hours with a dot, while in the formatter you use :
you do not have milliseconds in your string while you declared them in the formatter.
The following code should work:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa");
try {
Date date = dateFormat.parse("01-01-2037 01.00.00.000 AM");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}