Set a date from the subtraction of the same - Java [duplicate] - java

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.

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 !

Timezone getDSTSavings/observesDaylightTime equivalent in java 8 [duplicate]

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.

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

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