I want to convert TimeUnit to seconds or hours , I've asked a lot but do not get a satisfactory answer.
I've read on http://developer.android.com/reference/java/util/concurrent/TimeUnit.html#toHours(long) but I don't understand to use
example:
String strfileDate = "2012-04-19 15:15:00";
DateFormat formatter2 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date da =formatter2.parse(strFileDate);
long diffInMs = da.getTime() - new Date(System.currentTimeMillis()).getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
I want to convert valuse of "diffInSec" to Seconds, hours
convert day to hours, should be TimeUnit.DAYS.toHours(days).
for other type, change Enum value for source time unit.
Related
What does this date means: 1427856000472?
I have got this date from cursor.getString(4).
I need the receiving date of the message, but its format is not clear to me.
Kindly help, and thank you very much for sharing your knowledge.
Its in time in milliseconds .. you need to convert them to proper time.. following thing is what i followed.. it will return date month and year
where timestamp is equal to your time .. eg 1427856000472
final Calendar cal = Calendar.getInstance();
java.text.DateFormat df= DateFormat.getMediumDateFormat(mCtx);
java.text.DateFormat df1=DateFormat.getTimeFormat(mCtx);
String date = (df.format(timeStamp).toString());
String time=df1.format(timeStamp);
cal.setTimeInMillis(timeStamp);
int messageYear=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
int dates=cal.get(Calendar.DATE);
Locale locale=Locale.getDefault();
String monthName=cal.getDisplayName(Calendar.MONTH, Calendar.SHORT , locale);
String date = monthName+" "+dates+"\n"+time;
That time is the time elapsed since epoch(The Unix epoch is the number of seconds that have elapsed since January 1, 1970).
You can convert epoch time using this link:
http://www.epochconverter.com/
You can find the answer here in this post:
convert epoch time to date
Hope this helps.
I am trying to make my countdown timer show the seconds as 2 digits instead of one.
Example: 02 instead of 2.
I am using the code below, but I am not having any luck with the formatting.
long a = ((millisUntilFinished % 60000) / 1000);
String b = Long.toString(a);
SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm:ss:");
String time = myFormat.format(b);
The above code should work but if possible try to have your a value in milliseconds. The below code is working fine for me.
long a = 1395886625000L;
SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm:ss");
String time = myFormat.format(a);
I am building a basic logging utility class and would like to log the date and time down to the millisecond of when the log entry is created. I'm currently using:
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d )
which gives me '05-23 09:05:47'. I would like it to give me the milliseconds of when the log entry is created also and it does not appear that the DateFormat class supports millisecond retrieval.
Like the format "MM-dd hh:mm:ss:zzz" giving '05-23 09:05:47.447'.
Is it possible to do this using the DateFormat class (or a class like DateFormat)? I recognize it is possible to create another date removing the milliseconds part of this date and then subtracting the two and printing the difference but that's just silly. (:
Try this
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd hh:mm:ss SSS");
String dateToOutput = sdf.format(d);
While I think it's silly the DateFormat class doesn't allow easy formatted output of milliseconds, I realized that obtaining the milliseconds from the timestamp is actually quite easy. Every date object is representing a timestamp in milliseconds since 00:00 January 1, 1970 so the timestamp modulo 1000 gives the milliseconds.
I did
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d );
dateToOutput += "." + d.getTime() % 1000;
which, while not ideal, works fine and gives me '05-23 09:05:47.447'.
A 4 byte Long integer can give
(2^(32) - 1) = 4294967295
10 digit
Now, the time given by Location Manager in android is currently 13 digit long( like 1366588814000 ).
Can the number of digits increase in future as time progresses ? I don't know in what data type android stores the time.
The return type of the getTime() is long as mentioned in the docs. So you need not worry about the length of the digits. You can always convert that time to a human readable form like
long time = location.getTime();
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String text = sdf.format(date);
System.out.println(text);
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.