java - Date Timezone Conversion From String to Date to String - java

I have a routine which receives a date in the format yyyy, MMM dd, HH:mmin GMT time. I have to convert this String to a Date object and I do it with a SimpleDateFormat, but now I have to take that Date object and format it in GMT-5 using again a SimpleDateFormat, but the method is returning the same original String Date. Why? This is my routine:
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}
#Test
public void testDateConversion() {
String strDate = "2015, Aug 03, 23:50";
Date date = DateFormatter.parseDate(strDate, "yyyy, MMM dd, HH:mm");
String dateFormatted = DateFormatter.formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Error message:
org.junit.ComparisonFailure:
Expected :2015, Aug 03, 19:50
Actual :2015, Aug 03, 23:50

Solved by indicating the Timezone of the receiving date string:
public static TimeZone originTimeZone = TimeZone.getTimeZone("GMT"); // +Added
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(originTimeZone);// +Added
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}

Try changing the String you pass in to format, like so
#Test
public void testDateConversion() {
//also changed this from 23:50 to 19:50
String strDate = "Aug 03, 2015, 19:50";
Date date = parseDate(strDate, "MMM dd, yyyy, HH:mm");
String dateFormatted = formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Otherwise your code is formatting as expected. The problem is, you're passing the date string in pre-formatted, so your end isn't going to change from what you started with. Your unit test is failing because you're passing in "Aug 03, 2015, 23:50" and telling Junit to expect "Aug 03, 2015, 19:50";. When you pass in a String to Date like that, the time value isn't going to change at all

Related

How can I reformat date and time?

How can I convert 24 hours time format into 12 hours format? I know this question has been asked many times, but my problem is different. My current time is:
Tue Nov 07 18:44:47 GMT+05:00 2017
I just want 6:44 pm from my date time. I tried this:
private void convertTime(String time)
{
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
}
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Using hh will give you 12 hours format and HH 24 hour format. More details on documentation.
Edit:
Your initial format must be the following in order to parse your date string to a Date object:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'Z yyyy");
final Date dateObj = sdf.parse(time);
After that you can format time to your needs.
From SimpleDateFormat documentation:
"h:mm a": 12:08 PM
So the format you need for:
I just want 6:44 pm from my date time
is:
final SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date date = sdf1.parse(time);
final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String newDateString = sdf.format(date);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}

How to parse given string format to dd-MMM-yyyy hh:mm:ss:aa?

I can get the output as Wed May 11 15:36:08 IST 2016, but how do I convert the date to a string with the required format?
Required format is: 12-05-2016 16:05:08 pm
What I tried is,
public class Test {
public static void main(String args[]) throws ParseException{
String epoche="1462961108000";
Long initialLogTime = Long.valueOf(epoche);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(initialLogTime);
Calendar fromDateTime = calendar;
Calendar toDateTime = fromDateTime;
toDateTime.add(Calendar.MINUTE, 30);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss:aa");
String datestring = String.valueOf(fromDateTime.getTime());
String datestring1 = String.valueOf(toDateTime.getTime());
System.out.println(datestring); //here output is Wed May 11 15:36:08 IST 2016
System.out.println(datestring1); // here output is Wed May 11 15:36:08 IST 2016
Date dates = dateFormat.parse(datestring);
Date date1s = dateFormat.parse(datestring1);
System.out.println(dates);
System.out.println(date1s);
}
}
The error I am getting is:
Exception in thread "main" java.text.ParseException: Unparseable date: "Wed May 11 16:05:08 IST 2016"
at java.text.DateFormat.parse(DateFormat.java:357)
at test.Test.main(Test.java:27)
You need to format your dates accordingly. This shall help you
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
System.out.println(dateFormat.format(fromDateTime.getTime()));
System.out.println(dateFormat.format(toDateTime.getTime()));
if you are using java 8, you can use
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss:aa");
System.out.println(date.format(formatter));
Please try this
public static void main(String[] args) {
String epoche = "1462961108000";
Date date = new Date(Long.parseLong(epoche));
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
String strDate = sdf.format(date);
System.out.println(strDate);
}
Output
11-05-2016 15:35:08:PM
In Android, Pass String Like 11/10/2017 11:16:46 to function ConvertDateTime
public String ConvertUpdate(String strDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date d = simpleDateFormat.parse(strDate);
simpleDateFormat = new SimpleDateFormat("dd MMM yy hh:mm a");
return simpleDateFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
output
10 Nov 17 11:16 AM

Android Convert Ruby on Rails Timestamp to Date Time

I am building an Android App, Fetching Data from API that is build in Ruby On Rails Framework.
Here is my timestamp string
2000-01-01T12:00:00.000Z
I need to convert it to Readable date and time format i.e "Saturday January 1st, 2000 12:00 am"
You can try this:
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
}
String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
I had the same issue and I tried #real19 answer but it gave me errors.
I came with that then :
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
e.printStackTrace();
}
String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
Formatting gave me jeudi décembre 22, 2016 at 16:42 PM but you can adjust your DateFormat

Parse date in the format '2014-09-03T13:13:08Z'

Can you please help me to parse the date in the format "2014-09-03T13:13:08Z",
I have tried the format
public static Date getFormatedDate(String date)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
Date dateConverted = new Date();
try {
dateConverted = df.parse(date);
} catch ( ParseException e ) {
}
return dateConverted;
}
But it doesn't parse.
Your current date format works fine. You need to set Locale
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z",
Locale.ENGLISH);
eg:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.ENGLISH);
Date dateConverted = new Date();
try {
dateConverted = df.parse(date);
} catch (ParseException e) {
System.out.println("Error"+e);
}
System.out.println(dateConverted);
Out put:
Wed Oct 01 12:03:51 IST 2014

Java SimpleDateFormat: Unparseable Date exception

The code is as mentioned below:
public static void main(String[] args){
Date date = new Date();
DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");
try{
Date formattedDate = dateFormat.parse(date.toString());
System.out.println(formattedDate.toString());
}catch(ParseException parseEx){
parseEx.printStackTrace();
}
}
In code above, dateFormat.parse(date.toString()) is throwing unparseable date exception: Unparseable date: "Mon Jan 28 18:53:24 IST 2013
I am not able to figure out the reason for it.
Format the java.util.Date instance into String using SimpleDateFormat.format(java.util.Date)
Date date = new Date();
DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");
try {
Date formattedDate = dateFormat.parse(dateFormat.format(date));
System.out.println(formattedDate.toString());
} catch (ParseException parseEx) {
parseEx.printStackTrace();
}
Why would you want to convert a date to a string and parse it back to a date?
The reason your code fails is because you are trying to convert a full date with a formatter which only accepts dates in the dd-MMM-yyy-format.
public static void main(String[] args) throws ParseException {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat(
"EEE MMM d HH:mm:ss Z yyyy");
Date formattedDate = dateFormat.parse(date.toString());
System.out.println(formattedDate);
}
This is what you exactly want to do ...yes?

Categories