Convert EST time to local Time in java - java

I'm unable to convert (12/19/2012 8:57am EST) to local Time (now Indian Time).
While converting I'm getting wrong time (Dec 19 2012 11:27). I'm using the following code:
private void convertEdtToLocalTime(String pubDate)
{
//pubDate = 12/19/2012 8:57am EST;
String localPubDate;
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy HH:mma z");
TimeZone timeZone = TimeZone.getDefault();
sdf.setTimeZone(timeZone);
if (pubDate != null)
{
Date date = sdf.parse(pubDate);
sdf = new SimpleDateFormat("MMM dd yyyy HH:mm");
localPubDate = sdf.format(date);
}
}
catch (ParseException e)
{
}
}

You dont need to set the timeZone as the the time zone is already specified in the pubDate string. When you want to format using different SDF, the default timezone will convert it into default timezone itself. For eg. if you are in india, IST time = Dec 19 2012 19:27
private static void convertEdtToLocalTime(String pubDate)
{
//pubDate = 12/19/2012 8:57am EST;
String localPubDate=null;
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy HH:mma z");
// TimeZone timeZone = TimeZone.getDefault(); // No need to do it
// sdf.setTimeZone(timeZone);
if (pubDate != null)
{
Date date = sdf.parse(pubDate);
sdf = new SimpleDateFormat("MMM dd yyyy HH:mm");
localPubDate = sdf.format(date);
}
}
catch (ParseException e)
{
}
System.out.println(localPubDate);
}

Related

How to print date in GMT format? [duplicate]

I have a string "2014-07-02T17:12:36.488-01:00" which shows the Mountain time zone. I parsed this into java.util.date format. Now I need to convert this to GMT format. Can anyone help me??
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Object dd = null;
try {
dd=sdf.parseObject("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();`enter code here`
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:+ gmtDateFormat.format(dd));
There are a few problems in your code. For example, the format string doesn't match the actual format of the string you are parsing.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Object dd = null;
try {
dd = sdf.parse("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd));
To print the current date in whatever timezone you like, set the timezone you want to use on the SimpleDateFormat object. For example:
// Create a Date object set to the current date and time
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(now));
df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println("Current date and time in IST: " + df.format(now));

android utc time conversion issue

I need to convert a UTC time string into my local time, the code works fine in my PC when I run it on Eclipse, however, I get incorrect results on Android. I am stuck, can someone help.
Below is the code I am using, an example input would be "2017-04-24 1:00 AM", this will yield a result of "11:00 AM" which is what I am expecting, however, it always return "10:00 AM" on Android.
public String convertUTCTime(String utcTime) {
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm a");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date date = sdf.parse(utcTime);
sdf = new SimpleDateFormat("HH:mm a");
sdf.setTimeZone(tz);
String format = sdf.format(date);
return format;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Not applicable";
}
Look I prepared function:-
public static String getUTCDate(String timestamp, String DateFormat) {
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm aa", Locale.getDefault());
originalFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat targetFormat = new SimpleDateFormat(DateFormat, Locale.getDefault());
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
String formattedDate = null;
try {
date = originalFormat.parse(timestamp);
formattedDate = targetFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
Get the value by calling -
Utils.getUTCDate("2017-04-24 01:00 AM", "HH:mm a")

Unable to convert String to date, Unparseable date exception

I am passing dateInString as "2015-07-30T14:30:00-04:00". But
it is throwing
java.text.ParseException: Unparseable date:
"2015-07-30T14:30:00-04:00" (at offset 0)
Here is the sample code:
private Date convertStringToDate(String dateInString){
if(dateInString!=null){
Date convertedDate=null;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
TimeZone timeZone = TimeZone.getDefault();
formatter.setTimeZone(timeZone);
try {
convertedDate = formatter.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
else
return null;
}
I have solved it. Here is the date format-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Thanks everyone for your time :-)

Android Date format. from string to Date

I have a string like this: 2015-01-31 16:00:00 +0000 UTC, I want to parse this string to date object.
yyyy-MM-dd HH:mm:ss 'UTC'
but failed.
You can use the SimpleDateFormat for formatting the strings to date objects:
DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+z", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = originalFormat.parse("2015-01-31 16:00:00 +0000");
String formattedDate = targetFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}

Parse Datetime to JAVA Date

im getting the SQL Server datetime (1 Jan 2013) form the SqlRowSet like
while (rs.next()) {
myBean.setDateProp(rs.getString(4));
}
the type of myBean DateProp is java.util.Date, is there a way to convert (1 Jan 2013) to java Date representation.
i have tried the following code
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
Date date=new Date();
try {
date = sdf.parse("1 Jan 2013");
} catch (Exception e) {
e.printStackTrace();
}
and i get the ParseException
SEVERE: java.text.ParseException: Unparseable date: "1 Jan 2013"
any directions...
try this -
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
Date date=new Date();
try {
date = sdf.parse("1 Jan 2013");
}catch (Exception e) {
e.printStackTrace();
}
Try
while (rs.next()) {
myBean.setDateProp(rs.getDate(4));
}
#Test
public void test() throws ParseException {
String dateString = "1 Jan 2013";
String dateString2 = "11 Jan 2013";
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
sdf.parse(dateString);
sdf.parse(dateString2);
}
You need to modify your Date formatter string to
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
as your date string is in this format

Categories

Resources