Getting the Correct Time from A TimeStamp - android

I am recieving the following long 1389967092 as a time stamp. This should represent ...
GMT: Fri, 17 Jan 2014 13:58:12 GMT
OR
My time zone: 1/17/2014 2:58:12 PM GMT+1
However I am in GMT+1 but the time I want to show is the GMT representation. I am trying the following:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeStamp);
Date date = cal.getTime();
Log.d(Point.class.getSimpleName(), date.getHours() + ":" + date.getMinutes());
But this prints 12:58
What am I doing wrong?
EDIT - RESPONSE TO ANSWER
This the code I am using it prints 1969-12-13 10:54:48
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1389967092*1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.d(Point.class.getSimpleName(), sdf.format(calendar.getTime));

The default Calendar.getInstance returns the time according to your device's timezone. However, you may request a different timezone like this:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
In order to print your current time according to different timezone(s) then you may use SimpleDateFormat. For example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.d(TAG, sdf.format(new Date()));

Related

Getting exact time in ( HH:mm:sss a) format from milliseconds

I have to print time in my app in HH:mm:sss AM/PM from milliseconds which is 24 Hours later time .
so to get 24 hours later time to current time i used the code as
public Date roundToNext24Hour() {
Date date = new Date();
Calendar c = Calendar.getInstance();
c = new GregorianCalendar();
c.setTime(date);
c.add(Calendar.HOUR_OF_DAY, 24);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
now i convert this time in HH:mm:sss am/pm format
SimpleDateFormat df = new SimpleDateFormat("HH:mm:sss a");
timeString = df.format(Utility.roundToNext24Hour())
but problem is timestring is always returning time which is 12 hour later not 24 hour later.
please help..
Try below code.
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String str = df.format(date);

How to get standard format date in android

if i try the below code for eg:
i will set the date as Fri July 25 2014 10:00 AM. which gives date in milliseconds as 1402080056000,
now if i try to read the same milliseconds to date as below
long time = 1402080056000;
Date mydate = new Date(time);
mydate variable shows date as Sat Jun 25 00:10:56 IST 2014
String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));
with the above statement in DateTimeString i get date as Jun 25 , 2014 12:10:56 AM
How to read the datetime present in 1402080056000 to Fri July 25 2014 10:00 AM
Just need to work on the format string,
String dateTimeString=
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));
Explicitly set time zone:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));
Also, this would be useful Regarding Timezones and Java
try below code:
private String convertMilliToDate(long timestamp) {
DateFormat formatter = new SimpleDateFormat("YOUR DATE FORMAT");
// Create a calendar object that will convert the date and time value in
// milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return formatter.format(calendar.getTime());
}
Try this:
String date= DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(date_in_milis)).toString();
Date-time work is easier using the Joda-Time library, which works on Android.
long millis = 1402080056000L;
DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( millis, timeZoneIndia );
DateTime dateTimeFrance = dateTimeIndia.withZone( DateTimeZone.forID( "Europe/Paris" ) );
DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );
To parse a string as a date-time, search StackOverflow for "Joda parse" to find many examples.

SimpleDateFormat WeekDayNumber

#SuppressLint("SimpleDateFormat")
public String WeekDayNumber(){
long timeInMillis = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
SimpleDateFormat df = new SimpleDateFormat("c");
return df.format(cal.getTime());
}
This gives me weekday name of the date.
I want to get weekdaynumber like
"0" for sunday, "1" for monday ... "6" for saturday as string.
I've read the SimpleDateFormat documentation but stand-alone day of week is not so close.
I don't want to write a function like replace "Mon" to "1" because of various device locale.
How can I get the week day number of the date?
Use cal.get(Calendar.DAY_OF_WEEK):
long timeInMillis = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
Documentation
you could use the Calendar class in a Java action.
Some sample code:
SimpleDateFormat regularDateFormat = new SimpleDateFormat("dd:MM:yyyy");
Date date = regularDateFormat.parse("08:04:2010");
System.out.println("date: " + date.toString());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("day of week: " + calendar.get(Calendar.DAY_OF_WEEK));
This will produce the following output:
date: Thu Apr 08 00:00:00 CEST 2010
day of week: 5
You could easily build a java action that takes a date as input and returns the day numb

Calendar not returning GMT

I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.

Converting seconds to date time String

I have seconds from epoch time and want to convert it to Day-Month-Year HH:MM
I have tried following but it gives me wrong value.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
Above code is not working properly am i doing anything wrong here.
For example if seconds = 1299671538
then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
For example if seconds = 1299671538 then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
You have integer overflow. Just use the following (notice "L" after 1000 constant):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000L);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
or better use SimpleDateFormat class:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm");
String dateString = formatter.format(new Date(seconds * 1000L));
this will give you the following date string for your original seconds input:
Wednesday, March 9, 2011 13:52
You need to use
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
instead of
Calendar cal = Calendar.getInstance();
because UTC time from seconds depends on Timezone.
You don't need a calendar in this case, you can simply use the constructor new Date(1000 * seconds)
Then use a SimpleDateFormat to create a String to display it.
For a full explanation on using SimpleDateFormat go here.
The answer to this question though is that you need to use long values instead of ints.
new Date(1299674566000l)
If you don't believe me, run this:
int secondsInt = 1299674566;
System.out.println(new Date(secondsInt *1000));
long secondsLong = 1299674566;
System.out.println(new Date(secondsLong *1000));
I can confirm that answer from #Idolon is working fine, simple snippet is below...
long created = 1300563523;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
String dateString = formatter.format(new Date(created * 1000L));
using SimpleDateFormat is better way, change the format as you need.
Won't it work wihtout Calendar, like below? Haven't run this piece of code, but guess it should work .
CharSequence theDate = DateFormat.format("Day-Month-Year HH:MM", objDate);

Categories

Resources