I've been trying to find the answer to this for a while today and there's just so much contradictory information....
What I'd like to do is get a current unix timestamp in android, and then convert it to a format that allows me to getHours() and getMinutes().
I'm currently doing this:
int time = (int) (System.currentTimeMillis());
Timestamp ts = new Timestamp(time);
mHour = ts.getHours();
mMinute = ts.getMinutes();
But it's not giving me a correct value for hour or minute (it's returning 03:38 for the current East-coast time of 13:33).
This works:
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();
Just use the Java Calendar class.
Calendar c = new GregorianCalendar(); // This creates a Calendar instance with the current time
mHour = c.get(Calendar.HOUR);
mMinute = c.get(Calendar.MINUTE);
Also note that your Android emulator will return times in GMT for the current time. I advise testing this type of code on a real device.
int time = (int) (System.currentTimeMillis());
here you should use long instead of int.
Use Time class form Google it is the best for this kind of job specialy it has good performance not like Calendar.
Related
I am using following code to convert timezone (GMT-3) to device local timezone.
int hour=17,minute=0,day=12,month=6,year=2014;
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT-3"));
cal.set(year, (month-1), day,hour,minute);
cal.setTimeZone(TimeZone.getDefault());
Log.d("Time", cal.get(Calendar.DATE)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR)+" , "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+" "+cal.get(Calendar.AM_PM));
My local timezone is GMT+5:30
Expected result is
Time 13/5/2014, 1:30 0
But I am getting the result
12/5/2014 , 13:30 1
Sorry for you, GregorianCalendar is sometimes the hell. Your problem is following:
If you immediately set the timezone after having set the fields for year, month etc. then this mutable calendar class will only shift the timezone retaining the already set fields containing the local time. Those fields for year, month etc. will NOT be recalculated. This behaviour causes a shift on the global timeline represented by cal.getTime(), too.
In order to force the calendar object to recalculate the fields you need to call a getter. Watch out for following code and especially remove the comment marks to see the effect.
int hour = 17, minute = 0, day = 12, month = 6, year = 2014;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
TimeZone tz1 = TimeZone.getTimeZone("GMT-3");
sdf.setTimeZone(tz1);
Calendar cal = new GregorianCalendar(tz1);
cal.set(year, (month - 1), day, hour, minute);
// System.out.println(sdf.format(cal.getTime()));
// System.out.println("Hour=" + cal.get(Calendar.HOUR_OF_DAY));
TimeZone tz2 = TimeZone.getTimeZone("GMT+0530");
sdf.setTimeZone(tz2);
cal.setTimeZone(tz2);
System.out.println(sdf.format(cal.getTime()));
System.out.println("Hour=" + cal.get(Calendar.HOUR_OF_DAY));
Output with comment-disabled lines:
2014-06-12T17:00+0530
Hour=17
Output with enabled lines after having removed the comment marks:
2014-06-12T17:00-0300
Hour=17
2014-06-13T01:30+0530
Hour=1
In my Android app, I'm using the Time class. I understand getting the current time like this:
Time now = new Time();
now.setToNow();
but what I'm stumbling on is how to create a set value of 8pm in the Time class. It's not just: Time time8 = "2200";, because that's a String, and Time time8 = 2200; is an integer. So I'm stumped.
There are multiple ways to that, I think the most easiest for you would be to just set it directly:
set(int second, int minute, int hour, int monthDay, int month, int year)
Calendar rightNow = Calendar.getInstance();
int day = rightNow.get(Calendar.DAY_OF_MONTH);
int month = rightNow.get(Calendar.MONTH);
int year = rightNow.get(Calendar.YEAR);
Time time8 = new Time();
time8.set(0,0,22,day,month,year);
But i would only do it like that if you really want to use Time otherwise Calendar is much more useful
Calendar calendar8= Calendar.getInstance();
calendar8.set(Calendar.SECOND, 0);
calendar8.set(Calendar.MINUTE, 0);
calendar8.set(Calendar.HOUR_OF_DAY,22);
Could use the calendar class
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,5);
cal.set(Calendar.MINUTE,50);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
Date d = cal.getTime();
From android Dev.
A specific moment in time, with millisecond precision. Values typically come from currentTimeMillis(), and are always UTC, regardless of the system's time zone. This is often called "Unix time" or "epoch time".
You can do Date instead of time
Date newdate = new Date();
You can use calendar to break it down to what you actually need.
Can any one tell me how to set the current time in edittext box in android with out using Time-picker.
edt_time=(EditText) findViewById(R.id.editText_time);
Time t = new Time();
java.text.Time tf = android.text.format.Time.getCurrentTimezone(getApplicationContext());
edt_time.setText(tf.format(t));
Suggestions please.
Thanks for your precious time!..
Calendar c = Calendar.getInstance();
int mHour = c.get(Calendar.HOUR);
int mMinute = c.get(Calendar.MINUTE);
int mSeconds = c.get(Calendar.SECONDS);
EditText edt_time=(EditText) findViewById(R.id.editText_time);
edt_time.setText(mHour +":"+ mMinute +:+ mSeconds);
Similarly you can also get year, month,day etc.
The above code must work but anyway try this.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());
EditText edt_time=(EditText) findViewById(R.id.editText_time);
edt_time.setText(currentTime);
long time = System.currentTimeMillis(); // current time in miliseconds, conver it however you want
then put in editText,
editText.setText(String.valueOf(time));
I am doing a programme that stores the present time and date in "yyyy-MM-dd'T'HH:mm:ss.SSSZ" this format. and I am storing it in database as a string. when i am collecting the data i need the individual values like day, year, min, seconds etc.. how can i do this?
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String now = formatter.format(new Date());
Thank you,
Just use parse instead of format :
String dateFromDB = "";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date yourDate = parser.parse(dateFromDB);
And then you can can read any field you want using java.util.Date & Calendar API :
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
calendar.get(Calendar.DAY_OF_MONTH); //Day of the month :)
calendar.get(Calendar.SECOND); //number of seconds
//and so on
I hope it fits your needs
I'm suggesting that you store times in the DB as "timeInMillis". In my experience it simplifies code and it allows you to compare times values to eachother.
To store a time:
Calendar calendar = Calendar.getInstance(); // current time
long timeInMillis = calendar.getTimeInMillis();
mDb.saveTime (timeInMillis); // adjust this to work with your DB
To retrieve a time:
long timeInMillis = mDb.getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis (timeInMillis);
int milliSeconds = calendar.get(MILLISECOND);
//etc
There are these methods available to get the individual parts of a date
getDate()
getMinutes()
getHours()
getSeconds()
getMonth()
getTime()
getTimezoneOffset()
getYear()
Try using : int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
Here,
Calendar.HOUR_OF_DAY gives you the 24-hour time.
Calendar.HOUR gives you the 12-hour time.
I have a Date object. Now I want to add days to that Date object.
So how that can be done? Actually using Calendar object that can be done I know.
But in my case, I haven't used a calendar objects. Instead only used date object.
For Example, suppose I have a date object
Date dtStartDate=o.getStartDate();
int x=28;
Now what I want to do is to add 28 to this date object, means if the dtStartDate is 1 July 2011
then after adding 28, dtStartDate will be 29 July 2011.
Please suggest it to me.
Thanks in advance
You can add Day using below
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 1);
Here 1 is number of Day you can add.
OR
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
Toast.makeText(this, "" + dt, 5000).show();
May be your problem solved.
You could add the equivilent number of milliseconds to the time retrieved from Date, e.g.:
long millis = dtStartDate.getTime();
millis = millis + x*24*60*60*1000;
Date dtEndDate = new Date();
dtEndDate.setTime(millis);
You can easily do this in two simple ways my friend. First one is:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
and the second one is:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 24);
I think you would like to find this thing. Though there are so many persons are there who choose the first method.
Thanks.