Java convert utc time to Local time [duplicate] - java

This question already has answers here:
Java: How do you convert a UTC timestamp to local time?
(3 answers)
Closed 5 years ago.
Hi I have a problem : from server I get a time and it looks like this :
"date":"2017-05-24T07:56:22Z"
But now in my local time is 09:56:22 how I can convert this ?

First you need to parse the date, for example:
Instant instant = Instant.parse("2017-05-24T07:56:22Z");
Assuming your time zone is correctly set, you can then simply use:
LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime();
If you want to use a specific time zone instead of the system default time zone:
LocalTime localTime = instant.atZone(ZoneId.of("Europe/London")).toLocalTime();

Related

Java MYSQL Timestamp with millieconds issue [duplicate]

This question already has answers here:
Timestamp with a millisecond precision: How to save them in MySQL
(4 answers)
Closed 2 years ago.
I am using MySQL and have a column type TimeStamp. I am not able to store or retrieve milliseconds.
Any help please ? Thanks!
public static Date getBeginAndEndTime(String time) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmmssSSS");
LocalTime lt = LocalTime.parse(time, format);
return Timestamp.valueOf(lt.atDate(LocalDate.now()));
}
Ex: Time value coming as is "140833222" and when I stored in database it looks like "2020-08-26 14:08:33" missing last milliseconds "222".
I need to store and retrieve including milliseconds.
You should use the datatype BIGINT as it allows you to store 8 bytes. The DATE field is generally used for just that, Date and DateTime implementations like LocalDateTime. Since you're using a LocalDate and subsequently a LocalTime you're better off just using a DATE type. Your method effectively becomes;
public static Date getBeginAndEndTime(String time) {
// cache this, it's a thread-safe object
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmmssSSS");
return LocalTime.parse(time, format).atDate(LocalDate.now()));
}

How to convert UTC timestamp to asia/jakarta time [duplicate]

This question already has answers here:
Convert UTC date to current timezone
(5 answers)
Timezone conversion
(13 answers)
Closed 2 years ago.
I have string timestamp like
2020-05-25 08:03:24
I have tried to split the String using " " (a whitespace) as delimiter to get two Strings "2020-05-25" and "08:03:24". After that, I used substring to get the hours and added 7 to have jakarta time.
But when it is 17:01:00 for example, my calculated date is wrong.
The date given is in UTC.
I want to convert it become timezone [ASIA/Jakarta] how to convert utc timestamp become asia jakarta time?
You can use java.time if you are using Java 8 or higher.
The library provides handy possibilities of converting datetimes that don't have information about a time zone (like your example String) to a zone and handle conversions from one zone to another.
See this example:
public static void main(String[] args) {
// datetime string without a time zone or offset
String utcTimestamp = "2020-05-25 08:03:24";
// parse the datetime as it is to an object that only knows date and time (no zone)
LocalDateTime datetimeWithoutZone = LocalDateTime.parse(utcTimestamp,
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// convert it to a zone-aware datetime object by adding a zone
ZonedDateTime utcZdt = datetimeWithoutZone.atZone(ZoneId.of("UTC"));
// print the datetime in utc once
System.out.println(utcZdt);
// then convert the zoned datetime to a different time zone
ZonedDateTime asiaJakartaZdt = utcZdt.withZoneSameInstant(ZoneId.of("Asia/Jakarta"));
// and print the result
System.out.println(asiaJakartaZdt);
}
The output is
2020-05-25T08:03:24Z[UTC]
2020-05-25T15:03:24+07:00[Asia/Jakarta]

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.

Time Zone cannot be changed in Calendar util [duplicate]

This question already has answers here:
Working with various Calendar TimeZone in Java (without using Joda Time)
(3 answers)
Closed 9 years ago.
This sentence is supposed to get time information at Chicago time zone:
Calendar.getInstance(TimeZone.getTimeZone("America/Chicago")).getTime();
My problem is no matter what string I put in getTimeZone(), result would be changed.
Could anyone explain this situation?
The key thing to understand is that a java.util.Date represents UTC only - it has no time zone information. Time zones are presentation layer only - they are used to figure out how to display the time represented by the java.util.Date.
So if you use SimpleDateFormat or the Calendar.get(...) methods, the time zone will be taken into account.

Android get Current UTC time [duplicate]

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.

Categories

Resources