I have a date that I get from a server formatted in EST like this
05/07/2012 16:55:55 goes month/day/year then time
if the phone is not in EST how can I convert it to the timezone the phone is in?
it would be not problem if I got the time in milliseconds but I dont
EDIT:
ok now the time is not correct when formatting
String sTOC = oNewSTMsg.getAttribute("TOC").toString();
String timezoneID = TimeZone.getDefault().getID();
DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("EST"));
String newtimezoneID = TimeZone.getDefault().getID();
Date timestamp = null;
try{
timestamp = format.parse(sTOC);
format.setTimeZone(TimeZone.getDefault());
timezoneID = format.format(timestamp);
}catch(ParseException e){
}
I convert it to "EST" then format that time to the default TimeZone but the time is always off by an hour, not sure why?
Use the following code to get a UNIX timestamp:
String serverResp = "05/07/2012 16:55:55";
DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Date date = format.parse(serverResp);
Now you have the timestamp, which you know how to use.
Here's another question which covers conversion, in case you are curious: Android Convert Central Time to Local Time
Use the DateFormat class to parse the String into a Date. See the introduction to the API document here... http://docs.oracle.com/javase/1.5.0/docs/api/java/text/DateFormat.html
You can then create a Calendar for the Date...
Calendar cal = Calendar.getInstance().setTime(date);
And then you can change the timezone on the Calendar to a different timezone using setTimezone(). Or just get the time in milliseconds, using getTimeInMillis()
Using the Calendar, Date, and DateFormat classes should put you in the right direction.
See the Calendar documentation here... http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
Related
I have a string like this 210115 I want to represent it as 21:01:15 any ideas?.
I tried using Gregorian calendar but it adds date to it which I don't want
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
Date date = new Date();
try{
date = sdf.parse("210115");
}
catch(Exception e){
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
System.out.print(calendar.getTime());
Output is Thu Jan 01 21:01:15 UTC 1970 but what I want is just 21:01:15
Thanks.
To output a formatted date, you use another SimpleDateFormat object with a pattern with the format you want.
In this case, it sounds like you might want to use something like
SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println( outputFormat.format(date) );
So what you want is just a time, without time zone. I would recommend using the LocalTime class, which is exactly that, instead of the Date class.
LocalTime time = LocalTime.parse("210115", DateTimeFormatter.ofPattern("HHmmss"));
If u r getting the date string in "210115" this format and you want it in "21:01:15" format then why are you using date format.
Simply do string operation as:
String time="210115";
String newtime=time.substring(0,2)+":"+time.substring(2,4)+":"+time.substring(4,6);
System.out.println(newtime);
you will get the required format.21:01:15
I am working in date conversion in java in that i am using following code snippet to convert the UTC time to IST format.It is working properly in the local when i run it but when i deploy it in server its not converting , its displaying only the utc time itself.Is there any configuaration is needed in server side.Please help me out.
CODE SNIPPET:
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String pattern = "dd-MM-yyyy HH:mm:ss";
SimpleDateFormat formatter;
formatter = new SimpleDateFormat(pattern);
try {
String formattedDate = formatter.format(utcDate);
Date ISTDate = sdf.parse(formattedDate);
String ISTDateString = formatter.format(ISTDate);
return ISTDateString;
}
Java Date objects are already/always in UTC. Time Zone is something that is applied when formatting to text. A Date cannot (should not!) be in any time zone other than UTC.
So, the entire concept of converting utcDate to ISTDate is flawed.
(BTW: Bad name. Java conventions says it should be istDate)
Now, if you want the code to return the date as text in IST time zone, then you need to request that:
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // Or whatever IST is supposed to be
return formatter.format(utcDate);
Using Java 8 New API,
Instant s = Instant.parse("2019-09-28T18:12:17Z");
ZoneId.of("Asia/Kolkata");
LocalDateTime l = LocalDateTime.ofInstant(s, ZoneId.of("Asia/Kolkata"));
System.out.println(l);
I m facing a problem:I want to get current time of GMT TimeZone in long.
I m using the following code as given below:
TimeZone timeZoneGmt = TimeZone.getTimeZone("GMT");
long gmtCurrentTime = getCurrentTimeInSpecificTimeZone(timeZoneGmt);
public static long getCurrentTimeInSpecificTimeZone(TimeZone timeZone) {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(timeZone);
long finalValue = 0;
SimpleDateFormat sdf = new SimpleDateFormat(
"MMM dd yyyy hh:mm:ss:SSSaaa");
sdf.setTimeZone(timeZone);
Date finalDate = null;
String date = sdf.format(cal.getTime());
try {
finalDate = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finalValue = finalDate.getTime();
return finalValue;
}
As given in, above method
while formatting
String date = sdf.format(cal.getTime());
I m getting correct current time in GMT but as i do parsing by following code:
finalDate=sdf.parse(date);
Date got changed from current GMT time to 15:35:16 IST 2013 that is current time of my system.
I tried with Calendar as well in another way:
TimeZone timeZoneGmt=TimeZone.get("GMT");
Calendar calGmt = Calendar.getInstance();
calGmt.setTimeZone(timeZoneGmt);
long finalGmtValue = 0;
finalGmtValue = calGmt.getTimeInMillis();
System.out.println("Date......" + calGmt.getTime());
but still getting date as current time of my System Thu Jan 23 15:58:16 IST 2014 Not getting GMT current time.
You've misunderstood how Date works. A Date doesn't have a time zone - if you use Date.toString() you'll always see the default time zone. The long value in a Date is purely the number of milliseconds since the Unix epoch: it doesn't have any concept of time zone or calendar system.
If you want to represent a date and time in a particular time zone and calendar, use Calendar instead - but for getting "the current date and time as a long" you can just use System.currentTimeMillis(), which again does not have anything to do with the system time zone.
Additionally, even if you did want to do manipulation like this, you shouldn't be using string conversions. You're not conceptually performing any string conversions, so why introduce them?
If your aim is to display (as a string) the current date and time in a particular time zone, you should just use something like:
Date date = new Date(); // This will use the current time
SimpleDateFormat format = new SimpleDateFormat(...); // Pattern and locale
format.setTimeZone(zone); // The zone you want to display in
String formattedText = format.format(date);
When working with date and time APIs - particularly bad ones like the Java Calendar/Date API - it's very important that you understand exactly what each value in your system represents.
I am just starting with Java (Android) and got stuck on a Date formatting issue.
I have a small Form where you can enter a project name and choose a Start date on a Calendar. The Startdate and Projectname gets than entered into the database, after that the pre-defined Tasks get than entered automatically into the database.
Task 1 Due Date is Startdate,
Task 2 is the Startdate plus x days = DueDate2,
Task 3 is The DueDate2 plus x days = DueDate3
I have now come up with the below Sourcecode and everything works besides that I get the wrong Format of my Date. For some reason, my Format is correct in newDateStr, but when I parse it again to be a Date Object, the format changes and is than incorrect. I can't see my mistake, anyone can help?
My understanding is:
Set the Date format of the date entered (curFormat)
Set the target Date format (postFormater)
Parse your Date which is a String at this time, to turn it into a date Object (use curFormat)
Format this date to get target date format (use postFormater), now its a String again
Parse this again to get it back to be a date which is needed for the calendar
Use calendar instance, setTime(here the formated date) and add the x days
Format the Date to get target date format (use postFormater), now its a String again
Because I need a Date Object again, I have to parse it again.
// The format of your input date string
SimpleDateFormat curFormater = new SimpleDateFormat("MM/dd/yyyy");
// The format of your target date string
SimpleDateFormat postFormater = new SimpleDateFormat("dd-MM-yyyy");
// The calendar instance which adds a locale to the date
Calendar cal = Calendar.getInstance();
// Parse the string (pro.getStart()) to return a Date object
Date dateObj = curFormater.parse(pro.getStart());
// Format the Date dd-MM-yyyy
String newDateStr = postFormater.format(dateObj);
// Parse the string to return a Date object
Date Startdate = postFormater.parse(newDateStr);
while (cur.isAfterLast() == false)
{
Integer delayTime = cur.getInt(cur.getColumnIndex("DelayTime"));
if (flag == false)
{
dateInString = Startdate;
flag = true;
}else{
cal.setTime(dateInString);
// add the extra days
cal.add(Calendar.DAY_OF_MONTH, delayTime);
// Format the Date dd-MM-yyyy
newDateStr = postFormater.format(cal.getTime());
// Parse the string to return a Date object
dateInString = postFormater.parse(newDateStr);
Log.i("newDateStr Format",newDateStr.toString()); // 29-11-2012
Log.i("dateInString parse",dateInString.toString()); // Thu Nov 29 00:00:00 GMT 2012
I hope someone sees my mistake. Thank you very much in advance !
A java.util.Date object does not have any format in it or memory of the format you used to parse it. The output of its toString method, and hence what you get as output from dateInString.toString() will always be in the default JDK format that you are seeing: Thu Nov 29 00:00:00 GMT 2012
You have to use a formatter to convert it into a formatted string whenever you want to display it. You cannot "Format the Date object" so to say. (UI frameworks tend to have built in facilities for doing this automatically.)
Don't keep converting the Calendar back to a string each time you loop. Keep one and just accumulate the delays in place...
SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
// Set the date once
cal.setTime(fmt.parse(pro.getStart()));
while(!cur.isAfterLast()) {
// Accumulate additional days
Integer delayTime = cur.getInt(cur.getColumnIndex("DelayTime"));
cal.add(Calendar.DAY_OF_MONTH, delayTime);
}
String endDate = fmt.format(cal.getTime());
date here my problem:
String datetime = "2012-03-24 23:20:51";
I know that that string is in UTC timezone.
I need to convert this string to format "yyy-mm-dd'T'HH:mm:ssZ".
To do this I'm using following code:
SimpleDateFormat inFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
inFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date inDate = inFormatter.parse(datetime);
SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
outFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outFormatter.format(inDate);
The problem is that this code is running on a server with timezone UTC+1 and the result it gave me is this:
output = "2012-03-24T21:20:51+0000"
It removes 2 hours from the initial time and puts the UTC timestamp (0000).
Can you please help me solving this?
Thank you.
If the output format is UTC+1 then you should use that in the outformatter instead of UTC.
outFormatter.setTimeZone(TimeZone.getTimeZone("UTC+01:00"));
Also, if you don't want the +0000 at the end then remove the Z
SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");