How to get the start of the Day and the end of the day in Java

Sometimes it might be needed to determine the beginning of the day, or the beginning and the end of tomorrow. It might be primarily needed for filtering purposes.

Using class Instant

Using Java class java.time.Instant, we can define a moment at any point in time. Class Instant also ensures methods for arithmetic functions, which can be applied to the time value.

To instantiate the instance of class Instant we can use static method now():

Instant now = Instant.now();

Now we can apply time shift functions as well as arithmetic functions. To get the beginning of the current day, we can apply truncate operation, like below:

now.truncatedTo(ChronoUnit.DAYS);

Or if we want to get the beginning and the end of yesterday:

Instant yesterday = now.minus(1, ChronoUnit.DAYS);
Instant beginOfYesterday = yesterday.truncatedTo(ChronoUnit.DAYS);
Instant endOfYesterday = beginOfYesterday
                .plus(1, ChronoUnit.DAYS)
                .minus(1, ChronoUnit.NANOS);

2451
Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
Paydayloan
Guest
trackback

hydroxychloroquine long term

lack thrombolytic agents coverage

Quick Loans
Guest
Lisashoox
Guest
Lisashoox
Guest
Get A Loan
Guest
DouglasDrima
Guest
Online Lenders
Guest
trackback

generic viagra priligy

glass adjuvant therapy map

Henryheibe
Guest
Best Online Loans
Guest
DouglasDrima
Guest
DouglasDrima
Henryheibe
Guest
Henryheibe
Amyshoox
Guest
Lisashoox
Guest
trackback

acyclovir ointment

acyclovir ointment

Loans Online
Guest
trackback

amoxicillin 500 mg price

amoxicillin 500 mg price

trackback

interactions for donepezil

interactions for donepezil

trackback

amoxicillin 500mg capsules price

amoxicillin 500mg capsules price

trackback

zithromax capsules

zithromax capsules

Kiashoox
Guest
trackback

cefdinir side effects

cefdinir side effects

trackback

cephalexin medication

cephalexin medication

Getting A Loan
Guest
trackback

clindamycin hcl

clindamycin hcl

trackback

erythromycin ophthalmic ointment

erythromycin ophthalmic ointment

Kiashoox
Guest
trackback

azithromycin dihydrate

azithromycin dihydrate

Bad Credit
Guest
Amyshoox
Guest
Cash Advance
Guest
TrevorBulge
Guest
TrevorBulge
Loan Cash
Guest
Best Online Loans
Guest
Lisashoox
Guest
TrevorBulge
Guest
Payday Loan
Guest
TrevorBulge
Guest
TrevorBulge
Direct Lenders
Guest
Getting A Loan
Guest
trackback

cialis prices at walmart

cialis prices at walmart

trackback

tadalafil cost in india

tadalafil cost in india

Kiashoox
Guest
trackback

levitra 20mg coupon

levitra 20mg coupon