TimeZone extra hour with getDSTSavings() - android

I'm trying to show time saved at GMT+0 on serverside. Was using the code below to show relative time, however, now it gives an extra hour. How can I show relative time span that will work all around the world and timesaving sensitive?
When I don't add TimeZone.getDefault().getDSTSavings() it works as expected. However, before, it was working as expected when I add it.
Couldn't find a changelog for Calendar.getInstance() about it started to include daytime savings or not.
android.text.format.DateUtils.getRelativeTimeSpanString(1473846745000L,
Calendar.getInstance(TimeZone.getDefault()).getTimeInMillis() +
TimeZone.getDefault().getDSTSavings(),
android.text.format.DateUtils.SECOND_IN_MILLIS)

Related

Hide time detail in getRelativeDateTimeString call

I'm doing the following call in my Android app:
DateUtils.getRelativeDateTimeString(
context,
myTime,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0)
This returns strings like
3 hours ago, 5:58 PM
Yesterday, 2:23 PM
2 days ago, 9:02 AM
I only want to show the first part of this, the time is not interesting, especially not for a time which is 2 days in the past, or even weeks.
The last argument of this function is the flags argument. And there is the flag FORMAT_SHOW_TIME, but it seems that this flag is always enabled and disabling is not possible, or am I missing something?
What options do I have besides splitting the string by ',', which will for sure trigger other problems on some locales
You can use getRelativeTimeSpanString instead. It prints without absolute time. It does not support transitionResolution feature, though.
DateUtils.getRelativeTimeSpanString(
myTime,
now,
DateUtils.MINUTE_IN_MILLIS,
)
Here is a javadoc comment of RelativeDateTimeFormatter.getRelativeDateTimeString which is used by DateUtils.getRelativeDateTimeString
The function is similar to getRelativeTimeSpanString, but it always appends the absolute time to the relative time string to return '[relative time/date clause], [absolute time clause]
Related post: Format relative date to human readable format in Android using Android DateUtils API

How to Pick a period of time instead of specific time ReactNative

I'm using react-native Timepicker to pick a starting_time. but instead of ending_time i want to pick operation duration time, but it can not be done using timepicker. how can i pick a period of time instead of specific time?
now my app do this:
starting_time: 8 am
ending_time: 10 am
but what i want is:
starting_time: 8am
duration: 2 hours
I had the similar problem i fixed it using Time picker and Picker here is the screen shot.
if this solves your problem i can share the reusable components.

Getting time in Time format

I am currently trying to work with Location updating.
I've been using the below to work out the time-stamps of when the last update was processed:
String NewLocationTime = DateFormat.getTimeInstance().format(new Date());
However, to work out the difference between two times, I needed to parse it to a Time format. However, for some reason the Android Studio didn't recognize the DateTimeFormatter, and because I don't know in what format the String is going to come up, I am not quite sure what to put in the formatting either.
I believe it would be much easier if I was to be able to get a Time object straight away, so I can use something like:
long diffInMinutes = java.time.Duration.between(dateTime1, dateTime2).toMinutes();
Can anyone tell me how I get the Time object straight away, or why the Android Studio does not recognize the DateTimeFormatter?
Thank you
Use new Date().getTime() and what you get is the time in milliseconds and you can work with it as you want.
You need to use Date and use Date.getTime() for difference calculation. I do though recommend Joda-Time.
Also look at Calendar.
Time an sql aware wrapper around Date and there is no such thing as DateTime or DateTimeFormatter.

GoogleAnalytics HitBuilders.TimingBuilder

I'm using GA for an Android App.
I'm trying to use user timings to report how much time has passed for some actions in my code, so what I basically do is this:
At some point in the code I get System.currentTimeMillis(), at another point I do it again and subtract the latter from the former to get how much time has passed. I then report it to GA like this:
long time1 = System.currentTimeMillis();
...
long time2 = System.currentTimeMillis();
long timingValue = time2 - time1;
tracker.send(new HitBuilders.TimingBuilder()
.setCategory(timingCategory)
.setValue(timingValue)
.setVariable(timingVariable)
.setLabel(timingLabel)
.setCustomDimension(1,1)
.setCustomMetric(1, timingValue).build());
When I look at the "App Speed" section everything looks fine. It seems to report a logical average time in seconds like I expect.
The problem is that I want to use several dimensions (the secondary dimension is not enough) so I created all these timings as metrics as well so I can see them in a custom report. When I look at the report, the time I see there is 09:43:39 and I'm not sure what's the format here. Is it seconds:tenth of a second:hundredth of a second? And How can I see the average time of these metrics? I'm not sure if what I see is only the total amount of time or something else?
A Value of type time (for both events and custom metrics) should be passed as a whole number (no commas or decimals) representing seconds. So for example, 10 seconds should be 10, and 5 minutes should be 300, etc. Note that in the reports, it will be formatted as hh:mm:ss.

Android system time

in my application i need to get current Date and time, every time the user inputs data with it.
I know i can use System.currentTimeMillis(), but it can give me wrong time(because it gives system time, witch can be edited by user)
So i see the only way is to call server for current time, when the user makes data input. but i am not sure that internet connection is always awailable.
Is there any way to get current time (not system time) in android, without using internet connection?
If you don't want system time you need some other source then.
There are a few possibilities that I know:
Get it from web - Internet needed
Get it from router - enabled wifi needed (NTP)
Get it from GPS - GPS fix needed
All of these aren't very helpful I believe. I don't think you can find a way of getting current time without connecting so something externally.
In my opinion you should use system time and assume it's set correctly. When it's not your application shouldn't crash and should gently know user that he has wrong content because of wrong dates ...
I believe there's no way to get the current system time without the timezone.
A good approach would be getting the current system time first
long time= System.currentTimeMillis();
And then getting the correct TimeZone to handle it
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
long dateInMillis = System.currentTimeMillis();
String format = "yyyy-MM-dd HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateString = sdf.format(new Date(dateInMillis));
Use dateString as your variable which contains current date time as timestamp.
Well, I googled about your topic, and i got logic solution but not tested:
" Use The network-provided values ", in the android phone settings, it;a as shown in the picture bellow:
The screen I show is DateTimeSettings. The checkbox "Use network-provided values" is associated to the shared preference String KEY_AUTO_TIME = "auto_time"; and also to Settings.System.AUTO_TIME
This settings is observed by an observed called mAutoTimeObserver in the 2 network ServiceStateTrackers: GsmServiceStateTracker and CdmaServiceStateTracker.
Both implementations call a method called revertToNitz() when the settings becomes true. Apparently NITZ is the equivalent of NTP in the carrier world.
Bottom line: You can set the time to the value provided by the carrier thanks to revertToNitz(). Unfortunately, I haven't found a mechanism to get the network time. If you really need to do this, I'm afraid, you'll have to copy these ServiceStateTrackers implementations, catch the intent raised by the framework (I suppose), and add a getter to mSavedTime.
For more informations, i suggest you to check this link here
Use the ScheduledExecutorService with scheduleAtFixedRate to send you "clock ticks". If the user initiates an event and the number of accumulated "clock ticks" since the last event doesn't match the time change on the system clock, you're being lied to.
You don't need to know the correct time. You need to know the correct interval. This can be done with any periodic source, even a local one. (Timekeeping is two jobs: a metronome and a labeler for the intervals of the metronome. You don't want the system's labels because they can be made to lie, but the metronome ticks on even if the labels are changed.)
I'd recommend a relatively slow tick rate (<= 1 tick per minute) and rather sloppy comparisons (within 2%, maybe) since the various clocks may not be all that accurate.

Categories

Resources