This question already has answers here:
Converting UTC dates to other timezones
(7 answers)
Closed 5 years ago.
I need to get current time in UNIX format. But with System.currentTimeMillis() i get time, which is not time of my timezone. (My timezone is "GMT+3")
I want to convert my local time to UNIX format. How can i do that in Android?
I want to find simple and short way of it.
P.S.: In Android i can't use LocalDateTime.
I just needed adding an offset of my timezone. Function below was exactly what i wanted!
public static long getCurrentTimeInUnixFormat(){
return (System.currentTimeMillis()
+ TimeZone.getTimeZone("GMT+3").getOffset(System.currentTimeMillis())) / 1000;
}
Related
This question already has answers here:
Converting date to timestamp with timezone in Java
(3 answers)
Datetime behind an hour after insertion. Daylight savings
(2 answers)
Closed 2 years ago.
I have a 'simple' problem with Java and dates. I thought it was simple but I don't understand how to solve it ! I have try a lot of things. Here is the problem.
I save a date in my postgres sql database like this.
user.setCreationDate(Timestamp.valueOf((LocalDateTime.now())));
this date is store with one hour less than the real hour here.
When I want to use it, I would like to get in database and add the UTC +1 of my local zone.
I try this but always get the date -1h...
OffsetDateTime myNewDate = bddDate.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime()
Do you have an idea of how just get the local timezone exact hour ?
Thanks for your help !
This question already has answers here:
Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date
(5 answers)
How to show the difference in time after the daylight saving started in Java?
(2 answers)
Does Java 8's new Java Date Time API take care of DST?
(3 answers)
Get Daylight Saving Transition Dates For Time Zones in Java
(2 answers)
How to tackle daylight savings using TimeZone in Java
(8 answers)
Closed 2 years ago.
I have a ZonedDateTime instance namely say zonedDateTime for Europe/Berlin which is currently observing DST of an hour.
How do i know this instance observesDaylightTime or not : boolean value programmatically?
If it is then how much dst time applied currently in seconds? in this case it is one hour for berlin
I have tried below but no luck.
zonedDateTime.getOffset.getRules.isDaylightSavings(java.time.Instant.now())
zonedDateTime.getOffset.getRules.isDaylightSavings(zonedDateTime.toInstant())
It will be great if you can answer all TimeZone and SimpleTimeZone class methods and its equivalent in java8 time package.
Note: i have not asked for class comparision, I told methods.
e.g. TimeZone has getDisplayName() which is equivalent in java 8 like below
DateTimeFormatter.ofPattern("zzz").format(ZonedDateTime) // for short
DateTimeFormatter.ofPattern("zzzzz").format(ZonedDateTime) // for long
But above is good to have.
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
At the moment I have a String with the folowing format :
'DD/MM/YYYY' , and I'm willing to store this in a SQLite database as a date.
Since it is impossible to store it as a datetime, I've decided Integers would be the best choice, and someone told me about the Unix epoch solution. The thing is that I'm very unfamiliar with that, and I can't seem to convert a String into a unix epoch time...
Is there a way to directly convert a String with my format into a unix epoch time, or am I doing it wrong and should I change something?
I've read this question :Unix epoch time to Java Date object But still can not find my way out with my String...
Take a look at Parsing String date to date and adjust the format to yours.
If you have a java.util.Date just use http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() to get the UNIX timestamp as long.
If in doubt, read the docs! ;)
https://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.parse(<value>);
This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies
Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)
Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API
This question already has answers here:
Java - Subtract Days from date [duplicate]
(6 answers)
Closed 8 years ago.
I'm doing a calendar / organizer in java
This calendar / organizer features as inputs
Event setup time
Date / Time of event begins
Date / Time Event finish
I can not mark two events in the same period.
Solved this problem by consulting this link How can I determine if a date is between two dates in Java?
My doubts on how to determine date with setup time and date / time of the event beginning.
What I need is a date start (12/12/12 00:00:00) subtracting time setup (00/00/01 00:00:00) have this (12/12/11 00:00:00)
**sorry my bad english
You can use java.util.Date.getTime() to find a long representation of date and then do a simple comparison or arithematic.