Get Real Time – Not Device Set Time in android

You need to use the NTP (Network Time Protocol) protocol: Here is some code I found somewhere else… and I am using it. This uses the Apache Commons library, which can be installed using Gradle (adding a dependency on commons-net:commons-net:3.6) If you need a list of time servers, check: http://tf.nist.gov/service/time-servers.html Here is some Java Code … Read more

Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone

Nothing built in, my solution would be as follows : function tConvert (time) { // Check correct time format and split into components time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time]; if (time.length > 1) { // If time format correct time = time.slice (1); // Remove full string match value time[5] = +time[0] < 12 … Read more

How to convert milliseconds to “hh:mm:ss” format?

You were really close: String.format(“%02d:%02d:%02d”, TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) – TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line TimeUnit.MILLISECONDS.toSeconds(millis) – TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); You were converting hours to millisseconds using minutes instead of hours. BTW, I like your use of the TimeUnit API 🙂 Here’s some test code: public static void main(String[] args) throws ParseException { long millis = … Read more

Where can I find documentation on formatting a date in JavaScript?

I love 10 ways to format time and date using JavaScript and Working with Dates. Basically, you have three methods and you have to combine the strings for yourself: getDate() // Returns the date getMonth() // Returns the month getFullYear() // Returns the year Example: var d = new Date(); var curr_date = d.getDate(); var … Read more

Javascript add leading zeroes to date

Try this: http://jsfiddle.net/xA5B7/ var MyDate = new Date(); var MyDateString; MyDate.setDate(MyDate.getDate() + 20); MyDateString = (‘0’ + MyDate.getDate()).slice(-2) + “https://stackoverflow.com/” + (‘0’ + (MyDate.getMonth()+1)).slice(-2) + “https://stackoverflow.com/” + MyDate.getFullYear(); EDIT: To explain, .slice(-2) gives us the last two characters of the string. So no matter what, we can add “0” to the day or month, and … Read more