I need to convert an epoch time value to NSDate..
For exemple I have this epoch value (long) : 81915536
I'm interesting by the time value of the epoch value.
I have try that :
NSDate* date = [NSDate dateWithTimeIntervalSince1970:81915536];
but I got a 70's date....It's wrong.
But in a Java when I use this code, I obtain the right time value.
Date time = new Date(Long.valueOf(_epochTime));
String minutes = time.getMinutes()+"";
if(minutes.length()==1)
minutes="0"+minutes;
return time.getHours()+":"+minutes;
Someone can help me with this?
The current UNIX time is 1310407007 the seconds you gave are actually in the seventies, see here: http://www.unixtimestamp.com/index.php.
Also, it doesn't seem that you check the year in your Java code, only the hours and minutes.
One more thing, if you want a constant to be treated as long and not as int, add a postfix of L: 81915536L
Related
Problem:
I have got a directory of files, which have a creation date.
What I am trying to reach is to get the value of the creation date day, month and year.
However, I am working with a FileTime.
I was expecting to be able to call a GetMonth method or such.
Unfortunately, this is not possible, does someone know a nice solution to get the day/month/year of a FileTime?
What have I tried:
I have tried to convert this to the Date type. This is possible, but here are the day, month and year methods deprecated.
I have tried to use to get the milliseconds of the FileTime, but this did not feel like a pretty solution.
Final question:
How do I get the day, month and year of a FileTime?
Thanks in forward.
FileTime ➙ Instant ➙ ZonedDateTime
I'm assuming that you're using Java 8 or later. You must convert the FileTime to an Instant. You can then apply a ZoneId to the Instant to get a ZonedDateTime object for the time zone you want the date in. The below code converts the FileTime to date in India time.
val zonedTime = fileTime.toInstant().atZone(ZoneId.of("Asia/Kolkata"))
println(zonedTime.year)
println(zonedTime.monthValue)
println(zonedTime.dayOfMonth)
I'm using /checkins/recent endpoint. I was trying to get the check-in time. I find a json object createdAt. is that related with time ?
So, my question is how can I get the time from recent check-ins.
The Foursquare API docs for checkin object clearly state that createdAt represents the unix epoch time when the object was generated(time of activity). So, you can get the UTC time from the epoch. Add in the time zone offset, it denotes the minutes you need to add in the time to get local time.For example, the example checkin object in your question is from Bangladesh, so it has a offset of 360.
You haven't mentioned the language but from your tag list, I am assuming you have used Ruby so a simple way to do this in ruby would be.
require 'date'
Time.at(your_createdAt_value).utc.to_datetime
#For example from your created at value
Time.at(1389630660).utc.to_datetime
#This will make a datetime object of value 2014-01-13T16:31:00+00:00
For getting local time you can use the value of timeZoneOffset.
#To get local time add in the timeZoneOffSet
Time.at(your_createdAt_value).utc.to_datetime+Rational(timeZoneOffset_value,1440)
#The 1440 value is the number of minutes in a day. Rational calculates
#the fraction of the day by using the two values and adds it to the datetime object.
#for example,
Time.at(1389630660).utc.to_datetime+Rational(360,1440)
#This will make a datetime object of value 2014-01-13T22:31:00+00:00
You can format the object to the value you want by using formatting methods.
NSDate *createDate = [NSDate dateWithTimeIntervalSince1970:1376460694.103];
NSLog(#"createDate %#",createDate);
I am using the above code to get the current date and time,when I put break point at createDate,It shows correct time stamp value,but NSLog(#"createDate %#",createDate) statement is printing the date as 2013-08-14 06:11:34 +0000.
How to get the correct result?
The date is correct. When printing to the console the description of the date is used and that uses your system locale so it applies your time zone to the date before printing.
When you want to display the time you need to use a date formatter to convert the date into a string. The important part is setting the locale / time zone that the formatter uses.
Take a read of this and this.
I have spent quite a few hours and still unable to understand this:
Dim unix_time_at_midnight As Long
DateTime.DateFormat = "MM/dd/yyyy"
unix_time_at_midnight = DateTime.DateParse(DateTime.Date(unix_time*1000))/1000
where both unix_time_at_midnight and unix_time are long values. I understand DateTime.DateParse excepts a String and converts it to DateTime. What is (DateTime.Date(unix_time*1000))/1000 returning and what is its equivalent in Java? The requirement is to get the number of seconds since GMT midnight and I have successfully implemented it in Java. However, I would like to understand this particular line of code written in VB.net
EDIT: This method was written in Basic4Android and probably constitutes more of its libraries then vb.net. However, I have looked into each for details but unable to understand. Would appreciate if you could elaborate. Please see the links.
Take this:
DateTime.Date(unix_time*1000)
The documentation says:
Date (Ticks As Long) As String
Returns a string representation of the date (which is stored as ticks).
The date format can be set with the DateFormat keyword.
So that part returns a string representing the date.
It then uses DateTime.DateParse, which is documented as:
DateParse (Date As String) As Long
Parses the given date string and returns its ticks representation.
Taken together, this appears to take the ticks, multiplied by 1000, converted to a string that doesn't contain hour information which is parsed back to ticks which are divided by 1000.
The important thing to note is that the DateFormat set on the line before contains only the formatting for the date, no hours/minutes/seconds and smaller units of time exist in it. This means that the string returned essentially represents midnight of that date.
I'm looking to set the y-axis for a MSChart to be midnight to midnight, in either regular time format(i.e. - "1:30 AM") or military time. I figured out I can specify the y-axis format using ChartArea.AxisY.LabelStyle = new LabelStyle() { Format = "HH:mm" }, but cannot figure out what to set the minimum/maximum values to be.
Has anyone employed this format?
There are a few things you need to do to get this working properly.
MSChart accepts DateTime objects as Y values. You can emulate durations by doing this for each of your data points (assuming they are timespans or something convertible into a TimeSpan):
TimeSpan testSpan = TimeSpan.FromMinutes(5);
YourChart.Series(0).Points.AddY(new DateTime(testSpan.Ticks))
That will convert it into a datetime starting from the the beginning of CLR time (e.g. 1/1/0001 12:05:00 AM).
Then just use the label format "HH:mm" on the Y-axis.
<asp:ChartArea Name="VsChartArea">
<AxisY Minimum="0">
<LabelStyle Format="HH:mm" />
</AxisY>
</asp:ChartArea>
That should make it look like this:
To setup a custom interval (5 minutes):
<AxisY Minimum="0" IntervalType="Minutes" Interval="5">
Hope this helps!
I found a workaround since I never could get the formatting to work natively with DateTime values.
I eventually changed my Y axis data to be in integer format, with ranges from 0 to 2400(2359 really) to represent military time. I then updated the LabelStyle.Format to be "00:00" which renders my integer values into military time.
Yay for me. Hope this helps someone else.
If you use the Military time, do you notice that your scale is nolonger linear?