I am sending Sms from emulator control to emulator.. but it shows time in some different format. Can Anyone Help me understand that code or format. Here is the pic
Its probably using the System.currentTimeMillis() function for accessing the time, what returns the current time in milliseconds. If you want your date formatted, then use:
long time = System.currentTimeMillis();
String timeString = new Date(time).toLocaleString();
Or if you just need the time part from it, like in the example you have shown, then:
SimpleDateFormat formater = new SimpleDateFormat("h:mm a");
String timeString = formater.format(new Date(time)); //time is the current time as a long value;
As for how the date is stored:
System.currentTimeMillis()
Returns the difference, measured in milliseconds, between the current
time and midnight, January 1, 1970 UTC.
This means, that the long number you get, is the passed milliseconds since January 1, 1970.
From this value its pretty easy to count the current year, month day, etc...
As you can see in my previous example, you can conver this long value to a Date object, by passing it to the Date() constructor, and you can convert a Date object to a long value using:
long time = dateObject.getTime();
I hope this helps!
Related
I need the current time in milliseconds, so I use Calendar object to retrieve hours and minutes and convert them into milliseconds, but everytime it returns same value, that's the problem.
Calendar calendar = Calendar.getInstance();
int milliseconds = calendar.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
calendar.get(Calendar.MINUTE);
Callendar.getTimeInMillis() and System doesn't work for me cause I can't convert them to daytime. What I need is current daytime but in milliseconds.
use this
System.currentTimeMillis();
You can use
java.util.Calendar.getTimeInMillis()
to get time in millis directly for that particular Calendar instance. If you just need Current time use
System.currentTimeMillis();
https://developer.android.com/reference/java/util/Date.html#getTime()
(new Date()).getTime())
https://developer.android.com/reference/java/lang/System.html#currentTimeMillis()
System.currentTimeMillis());
Both these methods returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
try this use currentTimeMillis() it will Returns the current time in milliseconds.
Long time= System.currentTimeMillis();
Log.i("Time Class ", " Time value in millisecinds "+time);
Read currentTimeMillis.
Returns the current time in milliseconds. Note that while the unit of
time of the return value is a millisecond, the granularity of the
value depends on the underlying operating system and may be larger.
long getTIME= calendar.getTimeInMillis();
How do i convert ServerValue.TIMESTAMP into SimpleDateFormat("dd MM yyyy")
Date is deprecated so not able to use it , is their is any way to use calender
You cannot use Server.TIMESTAMP to get a date. The doc says:
A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) as determined by the Firebase servers
This means that when you setValue() or updateChildren(), you can put this constant in the Map to tell the server to put the epoch time in that node instead. For example:
mRef = FirebaseDatabase.getInstance().child("whatever/path/in/your/database");
mRef.setValue( Server.TIMESTAMP );
This will set in <your Firebase>/whatever/path/in/your/database a long that will look like 149141530600. This is the current epoch time I fetched while writing this answer. It corresponds to the number of milliseconds passed since january 1st 1970 to when I copied the value. Then, if you have a listener to that node, you can get the calendar using:
Long time = dataSnapshot.getValue(Long.class);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(time);
If you only want the time the server is set to (saving it in the database is pointless), you can use the special node:
`FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset");`
A listener to this node returns a Double that represents an approximative offset between the device time and the server time. You can then set the Calendar using:
calendar.setTimeInMillis(System.currentTimeMillis() + offset);
You can use the following.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ServerValue.TIMESTAMP);
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy",Locale.US);
fmt.format(cal.getTime()); //This returns a string formatted in the above way.
If ServerValue.TIMESTAMP is returned as a string, you can parse the string using Long.parseLong(Server.TIMESTAMP);
date= Calendar.getInstance();
Date currentDate = date.getTime();
String sDate = currentDate.toString();
This returns time EST. I need to change it to Arizona time which is tricky because Arizona does not have daylight savings time. Is there a short cut to making the changes or do I need to query a calendar to subtract two hours when Arizona is on MST and three hours when PST.
This returns time EST.
Well, Date.toString() will, if you're in EST at the moment. It's not part of the data stored within the Date - that's just an instant in time, with no idea what time zone or calendar system it might have started off in.
Your first two lines would be more simply written as:
Date currentDate = new Date();
You should use a DateFormat to convert the Date into a String. You can specify the time zone you want to use there. Do not start performing any arithmetic on the date yourself to add/remove offsets - that's a sign that you're heading in the wrong direction.
I am having following code to convert milliseconds to Android Date object.
Date dateObj = new Date(milli);
But problem is that my milliseconds value is having GMT value added in it before i pass it to Date class, add when i print this date object i can see that date object is again adding GMT value in the milliseconds value and because of that my date is displayed as wrong.
So how can i generate Date object with out considering GMT value in it.
For example my milliseconds are 1385569800000 which is getting printed as below:
Wed, 27 Nov 2013 22:00:00 --> +5.30
But the current value of this time stamp without adding GMT is:
Wed, 27 Nov 2013 16:30:00
*UPDAE*
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
So basically i want to create Date object which has proper date time value in it with out adding extra GMT time added in it.
A Date is always in UTC. No need to change that.
When printing the date value, use SimpleDateFormat and call setTimeZone() on it before formatting the output string.
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
TimerTask is just a task and not its scheduling. Timer accepts a Date object for scheduling. The Date is in UTC there as well.
try my code if you a
long currentTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
currentTime -= offsetInMillis;
Date date = new Date(currentTime);
it is work for me
You can try with joda-time API.
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.
http://joda-time.sourceforge.net/key_instant.html
A Date object simply represents a moment in time. Imagine you're on the phone to someone on a different continent, and you say "3...2...1...NOW!". That "NOW" is the same moment for both of you, even though for one person it's 9am and for the other it's 4pm.
You're creating a Date representing the moment 1385569800000 milliseconds after the Java epoch (the beginning of 1970, GMT). That is your "NOW", and it's fixed and unchanging. What it looks like converted into text, however, depends on which timezone you want to display it for. Java defaults to using GMT, which would be right if you were in Britain during the winter, but for (I'm guessing) India you want it in a different time zone. Laalto's answer shows you how to do that.
here is the code,that worked like charm for me:
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
I know this is very simple question but I am not able to do it.
I have a code that gets current time but this time is not accurate.
booking.CreateDateTime = DateTime.Now.ToUniversalTime();
When I am booking at 12:00 then in database stores 1:00 that means 1 hour difference.
How can I get accurate time?
Use System.currentTimeMillis() to get the current GMT time in mili seconds since epoch.
Then you can use this value to create a new Date or Calendar object and localize it wherever the user is.
I'm not familiar with what you have there, but ToUniversalTime suggests to me that this is adjusting your time to some fixed time zone (probably GMT)
Use a Date to get the time right now, and then a Calendar to do any time zone changes on it that you want.
Example, assuming CreateDateTime is actually a string of what you said it was:
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
booking.CreateDateTime = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));