Best way of storing Date & Time in Kotlin [duplicate] - java

This question already has answers here:
Should I use java.util.Date or switch to java.time.LocalDate
(3 answers)
Closed 2 years ago.
I'm honestly kinda lost.
I need to store date & time for simple usage, for example storing in the database, sectioned recycler view etc, but there are too many options of how to save data&time, and every article say one is better than the other.
There are Date, Calendar, LocalDateTime, Instant etc
My question is what is the current best way of storing date and time?

Best way to store date and time in millisecond. Convert your date and time to millisecond and store it.

Related

Java getting number of days between two dates [duplicate]

This question already has answers here:
Java, Calculate the number of days between two dates [duplicate]
(10 answers)
Closed 1 year ago.
I am trying to calculate the number of days between two dates.
Even though I found many similar questions, I just cannot come up with a solution.
Date lastpickup = (Date) section_userdata.get("lastpickup");
Date today = new Date();
Instant instant_lastpickup = lastpickup.toInstant().truncatedTo(ChronoUnit.DAYS);
Instant instant_today = today.toInstant().truncatedTo(ChronoUnit.DAYS);
This is my code at the moment.
A date is read from a config and should be compared to the actual date.
With the code I have I am able to determine whether the date is the same or not, but I want to know which amount of days (ideally as an Integer) is between those two.
I want to look at the calendar days, not 24h rhythm.
Well, it was way easier than I thought.
Here's my solution
long daysCount = ChronoUnit.DAYS.between(instant_lastpickup, instant_today);

Java - date time is always -1h from the local time [duplicate]

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 !

Getting time of special timezone in UNIX-time format. Android [duplicate]

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;
}

Transform a String in unix time java android [duplicate]

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>);

get today's date and time as string [duplicate]

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

Categories