How to print date in GMT format? [duplicate] - java

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));

Related

Date changing from one format to another format

I am trying to parse the date from one format to another format, but getting the parse exception. Please help me out on this issue.
String Orgdate= "2016-11-14T11:12:13";
java.util.Date tardate = null;
SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/mm/yyyy HH24:MI:SS");//Exception is in this line.
try {
targetdateformat = dateFormat2.parse(Orgdate);
} catch (ParseException e) {
e.printStackTrace();
}
Above code giving me date but different format,i am looking for date like below mention.
('14/11/2016 11:12:13')
Time format is 24 Hrs.
why not just do a simple hardcode to get the format you would like. For example...
String date = "2016-11-14T11:12:13";
String newDate = date.charAt(8) + "" + date.charAt(9) + "/" +
date.charAt(5) + "" + date.charAt(6) + "/" +
date.substring(0, 4) + " " + date.substring(11, 19);
System.out.println(newDate);
You need to parse to Date then format the date string to convert it into another format
String Orgdate= "2016-11-14T11:12:13";
java.util.Date tardate = null;
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
tardate = dateFormat2.parse(Orgdate);
System.out.println(tardate); // Mon Nov 14 11:12:13 UTC 2016
} catch (ParseException e) {
e.printStackTrace();
}
String formatted = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(tardate);
System.out.println(formatted); 14/11/2016 11:12:13
If you are using Java8 you can try below
LocalDateTime dateTime = LocalDateTime.parse("2016-11-14T11:12:13", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
String formattedDate = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")); // 14/11/2016 11:12:13
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date a = sdf.parse("2016-08-12T08:29:47");
sdf.applyPattern("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.toPattern()); // to see if new pattern applied
System.out.println(sdf.format(a)); // get output in desired format

Incorrect UNIX timestamp from String Date

I am trying to convert a date in String format into UNIX timestamp, I am able to convert it but when I check the timestamp it displays incorrect date.
I am using the following code to convert a Date in String to Unix timestamp:
String selected_date = "16/11/2015 1:34 am";
datetime.setText(selected_date);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
Date date = null;
try {
date = dateFormat.parse(selected_date);
} catch (ParseException e) {
e.printStackTrace();
}
long unixTime = (long)date.getTime()/1000;
The output UNIX timestamp is: 1460309640
But when I convert that timestamp using a web tool it returns: 4/11/2016, 1:34:00 AM
The format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
is not compatible with the string
String selected_date = "16/11/2015 1:34 am";
16 can't be a month!
2015 is not a year in two digits format
The right format seems to be (not sure if kk or KK depending on hours if 0 based or not)
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy kk:mm a");

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();
}

Android SimpleDateFormat always returns wrong week day

I have the following code:
String s = "08-12-2014 05:00:00"
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("EEEE, HH:mm a");
Date oneWayTripDate = null;
try {
oneWayTripDate = inputFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
String datetime = outputFormat.format(oneWayTripDate);
but for some weird reason it always returns the wrong day of the week. what am i doing wrong?
The input SimpleDateFormat pattern is wrong. Given the date 08-12-2014 05:00:00 with the year part at the end, and assuming 08 is the month, the format should be:
SimpleDateFormat inputFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH);
See the Javadoc of SimpleDateFormat for how to define date patterns.

Convert EST time to local Time in 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);
}

Categories

Resources