Android get Current UTC time [duplicate] - java

This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 9 years ago.
What is the function to get the current UTC time. I have tried with System.getCurrentTime but i get the current date and time of the device.
Thanks

System.currentTimeMillis() does give you the number of milliseconds since January 1, 1970 00:00:00 UTC. The reason you see local times might be because you convert a Date instance to a string before using it. You can use DateFormats to convert Dates to Strings in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
Also see this related question.

Related

get Date without time in java [duplicate]

This question already has answers here:
LocalDate to java.util.Date and vice versa simplest conversion? [duplicate]
(7 answers)
How do I get a Date without time in Java?
(23 answers)
Closed 3 years ago.
I want to get a Date without time, but always failed.
below is my codes:
long curLong = System.currentTimeMillis();
curLong = curLong - curLong % TimeUnit.DAYS.toMillis(1);
Date date = new Date(curLong);
System.out.println("date = " + date);
the output:
date = Mon Oct 28 08:00:00 CST 2019
anyone knows why? Thank you
It is not recommended to use java.util.Date anymore. It was called Date but doesn't necessarily hold only the date information but information about the time additionally.
Use this:
LocalDate today = LocalDate.now();
and print it as
System.out.println(today.format(DateTimeFormatter.ISO_DATE);
using the ISO date format. You can define your own formatting pattern using a
DateTimeFormatter.ofPattern("dd.MM.yyyy");
for example.
You can use java.time.LocalDate.now() to get just the date.
Anyway, your case doesn't work as you expect because you are doing nothing to remove the time from the date: you are just "repressing" it, that's why it's zero. If you want to continue this way you could always substring it (substring the Date.toString() of course I meant).
Hope I helped.
java.util.Date's javadoc states:
The class Date represents a specific instant in time, with millisecond precision.
Thats why you have date with time
If you want a date you can use : java.time.LocalDate.now() (Java 8+)
First of all, stop using the old java.util.Date. The new Java 8 date and time API has much better classes for all date and time operations.
The LocalDate class does exactly what you want.
The current date can be obtained by LocalDate.now().
It also has a lot of facilities to add and subtract days, months etc. and it takes into consideration all the calendar special cases for you.

How to Change time format of date string using timeoffset [duplicate]

This question already has answers here:
Strange Java Timezone Date Conversion Problem
(3 answers)
Closed 4 years ago.
I am trying to convert date string to UTC format from another time zone in Java. we have only time zone offset like "-06:00". Can any one help me how to convert the date time to UTC format using time zone offset.
Thanks
This for java version 1.7 . I have tried with following snippet but receiving the same input as output.
String dateInString = "02/04/2019 18:17:15";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(dateInString);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("+5:30"));
String dateStr = dateFormat.format(date);
System.out.println(dateStr);
Output
02/04/2019 18:17:15
Unfortunately, TimeZone.getTimeZone(String ID) returns:
the specified TimeZone, or the GMT zone if the given ID cannot be understood.
The "+5:30" time zone cannot be understood, so you get GMT.
Change to "GMT+5:30" will make your code work, i.e. it'll print:
02/04/2019 23:47:15
See the javadoc of TimeZone for valid ID syntax:
The syntax of a custom time zone ID is:
CustomID:
GMT Sign Hours : Minutes
GMT Sign Hours Minutes
GMT Sign Hours
Sign: one of
+ -
Hours:
Digit
Digit Digit
Minutes:
Digit Digit
Digit: one of
0 1 2 3 4 5 6 7 8 9
As you can see, it must always start with GMT.

Difference between hours (MM/dd/yyyy HH:mm:ss.SSS) [duplicate]

This question already has answers here:
How to find difference between two Joda-Time DateTimes in minutes
(4 answers)
Closed 7 years ago.
I have to implement a java program to make the difference between two hours. The problem is that my "hour pattern" is like this:
MM/dd/yyyy HH:mm:ss.SSS
I have to consider ms, too.
I tried using joda:
String sDateStart = "2015/10/14 22:37:28.648";
String sDateFiish = "2015/10/14 22:39:13.573";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date dt1 = format.parse(sDateFiish);
Date dt2 = format.parse(sDateStart);
DateTime start = new DateTime(dt1);
DateTime finish = new DateTime(dt2);
System.out.println(Days.daysBetween(start, finish));
System.out.println(Minutes.minutesBetween(start, finish));
System.out.println(Seconds.secondsBetween(start, finish));
Is there any method to consider the ms?
If you are using Java API itself. In Date class we have getTime method which gives time in millisecond
getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
System.out.println((long)(dt1.getTime() - dt2.getTime()));
If you wanna continue using Joda Time API use Duration. as mentioned by Sotirios Delimanolis

Converting long time to a java date [duplicate]

This question already has answers here:
Converting Integer time stamp into java date [duplicate]
(3 answers)
Closed 8 years ago.
I get a date from a request with this format > 1413972425000
When I execute in JavaScript
new Date(1413972425000)
the result is
Wed Oct 22 2014 11:07:05 GMT+0100 (GMT Daylight Time)
So.. What I want is get the datetime but in Java and I don't know how can I do it.
Thanks.
1413972425000 isn't a int value. It is too large for int. You can use it as a long value. 1413972425000L
You can use
Date date=new Date(1413972425000L); // accept long value.
System.out.println(date);
You can use new Date(1413972425000L) to convert long to date. Note the appended L to the numeric value.

Converting Integer time stamp into java date [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
I have following time stamp in Integer form
1333125342
I can convert it using SQL:
select DATEADD(ss, FlOOR(1333089223/86400)*86400, '1970-01-01 00:00:00') AS Date
How to convert it in java? So that it would return value:
3/30/12 12:18:43 PM
Assuming its the time since 1/1/1970 in seconds. you can try
String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(1333125342 * 1000L));
if it is milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT
then simply use
new java.util.Date(millis);
and if you need it in particular format
3/30/12 12:18:43 PM
then use SimpleDateFormat to format the Date to desired formatted String
That timestamp contains the seconds elapsed since 1970-1-1 0:00 UTC.
To convert it to a Java Date instantiate a new Date Object (see Java doc) and invoke setTime() on that. Note, that setTime expects milliseconds instead of seconds, so you would have to multiply your timestamp by 1000.
The toString() method yields something readable.

Categories

Resources