This is my code:
long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime); // if you really have long
String result = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
Is it possible to add milliseconds and nanoseconds to the date format ?
You can add milliseconds by adding SSS at the end, such as the format will be HH:mm:ss.SSS.
There is no reference in the SimpleDateFormat to nanoseconds. Usually System.nanoTime() is used for performance debugging and not for display purposes.
String result = new SimpleDateFormat("HH:mm:ss:SSS").format(date);
"HH:mm:ss:SSS" //SSS stands for milliseconds
Check SimpleDateFormat API for more info
String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
timeStamp = timeStamp + ":" + System.nanoTime();
This gave me output,
24:03:2014_18:24:09:890:2018826751601473
Related
I am trying to use joda API in my app for playing with Date object.
In some activity i am storing joda datetime in sharedpreference using following code
prefsEdit.putLong(context.getString(R.string.last_status_change_time_key) , DateTime.now().getMillis());
Now in some other activity, i m fetching this stored preference and calculating the difference between dates using following code
long lastStatusChangeTime = objSharedPref.GetAppLongPrefByKey(R.string.last_status_change_time_key);
DateTime now = DateTime.now();
DateTime dateTime = new DateTime(lastStatusChangeTime);
Seconds seconds = Seconds.secondsBetween(now, dateTime);
int n = seconds.getSeconds();
The code always return me values in minus eg -31 , -12 etc..
It is not calculating the difference correctly .
What is missing ??
The declaration of secondsBetween() is:
secondsBetween(ReadableInstant start, ReadableInstant end)
In order to have positive result the start date should be a date before the end date.
It is because secondsBetween() doesn't return an absolute value.
In your example dateTime is obviously before now, so in order to get positive values the invocation should be:
Seconds seconds = Seconds.secondsBetween(dateTime, now);
instead of:
Seconds seconds = Seconds.secondsBetween(now, dateTime); // <- wrong order as `startDate` parameter is a date after `endDate` parameter
and your code could be:
long lastStatusChangeTime = objSharedPref.GetAppLongPrefByKey(R.string.last_status_change_time_key);
DateTime now = DateTime.now();
DateTime dateTime = new DateTime(lastStatusChangeTime);
Seconds seconds = Seconds.secondsBetween(dateTime, now); // <-- Here is the difference
int n = seconds.getSeconds();
Just use native Java code.
long oldTime = Calendar.getInstance().getTime().getTime();
Thread.sleep(10*1000);
long newTime = Calendar.getInstance().getTime().getTime();
long diffInMillisecods = newTime - oldTime;
long diffInSeconds = diffInMillisecods / 1000;
This is my code:
long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime); // if you really have long
String result = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
Is it possible to add milliseconds and nanoseconds to the date format ?
You can add milliseconds by adding SSS at the end, such as the format will be HH:mm:ss.SSS.
There is no reference in the SimpleDateFormat to nanoseconds. Usually System.nanoTime() is used for performance debugging and not for display purposes.
String result = new SimpleDateFormat("HH:mm:ss:SSS").format(date);
"HH:mm:ss:SSS" //SSS stands for milliseconds
Check SimpleDateFormat API for more info
String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
timeStamp = timeStamp + ":" + System.nanoTime();
This gave me output,
24:03:2014_18:24:09:890:2018826751601473
I have a number representing the number of nanoseconds since 12:00 a.m., January 1, 1904, universal time. I wish to instantiate a java.util.Date object representing that date. How should I proceed?
You first need to convert your number representing nanoseconds to milliseconds.
Then for the given date string, get the total number of milliseconds since the unix time Epoch, and then add the number earlier converted to milliseconds to it.
Here's the working code:
String target = "1904/01/01 12:00 AM"; // Your given date string
long nanoseconds = ...; // nanoseconds since target time that you want to convert to java.util.Date
long millis = TimeUnit.MILLISECONDS.convert(nanoseconds, TimeUnit.NANOSECONDS);
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(target);
long newTimeInmillis = date.getTime() + millis;
Date date2 = new Date(newTimeInmillis);
System.out.println(date2);
Add an import java.util.concurrent.TimeUnit;.
I think it is trivial:
final GregorianCalendar startDate = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
startDate.clear();
startDate.set(1904, Calendar.JANUARY, 1);
final long startMillis = startDate.getTimeInMillis();
new Date(nanos / 1000 / 1000 + startMillis)
Date date = new Date(new Date().getTime()-(time in nanoseconds/(1000*1000)));
what's wrong in using this? I tested with a value of "time in nanoseconds" for June 8th 1926 and it works. and date format has nothing to do with it, the underlying milliseconds value representing the time is what is required.
First of all java.util.Date
Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
So, if you have variable milliseconds
Somehow calculate number of milliseconds between your date and Jan 1, 1970 (Unix epoch) diff
Use Date(long) constructor
new Date(milliseconds - diff);
How can I convert the difference of the current time a given time to create a string with the time format: HH:mm ? ex. 18:36
I did the following but, it is not 24Hour format, it will add AM/PM to the end, and it is 3 hours off.
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(time);
java.sql.Timestamp ts2 = new java.sql.Timestamp(parsedDate.getTime());
long nowTime = ts1.getTime();
long givenTime = ts2.getTime();
long timeDiff = givenTime - nowTime;
//convert to string
java.util.Date d = new java.util.Date(timeDiff);
result = DateFormat.getTimeInstance(DateFormat.SHORT).format(d);
//Outputs: 6:56 PM for example
Once easy thing you can do is call getTime() for both dates and then subtract them like so:
long timeDiff = today.getTime() - ts1.getTime()
That should give you the difference in miliseconds between the two times. After that you know that one second is 1k miliseconds, 1min i 60s, 1h is 60 minutes and so on.
Take a look at Commons Lang DurationFormatUtils.
Or Joda-Time's PeriodFormatter.
How do I convert 08:48 PM formatted string into SQL Unixtime?
Java 1.5
You need java.text.SimpleDateFormat with the hh:mm a pattern (0-12 hours, minutes, AM/PM marker). Click the link to see the Javadoc with detailed pattern explanations.
String time = "08:48 PM";
Date date = new SimpleDateFormat("hh:mm a").parse(time);
long timestampMillis = date.getTime();
long unixTimestamp = timestampMillis / 1000;
If you actually want to store this in a SQL TIME/TIMESTAMP/DATETIME field with help of JDBC, then wrap it in a java.sql.Time and use PreparedStatement#setTime() to save it.
Time time = new Time(timestampMillis); // Yes, with millis!
preparedStatement.setTime(1, time);
// ...
Assuming you still want today's date, try
String today = new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
long timestamp = new SimpleDateFormat("yyyy-MM-dd hh:mm a").parse(today + "08:48 PM").getTime() / 1000;