Solved

How to use java8 date api to get dayofmonth,dayofweek,yesterday date

Posted on 2016-08-23
8
52 Views
Last Modified: 2016-08-25
Hi Experts,
using java8 how can i display these date intervals...

DayOfMonth -2016-08-01(start day of month)
DayOfWeek  -2016-08-21(week start date)
Yesterday  -2016-08-22

Thanks,
0
Comment
Question by:srikotesh
  • 4
  • 3
8 Comments
 
LVL 17

Assisted Solution

by:xtermie
xtermie earned 100 total points
ID: 41766655
Java time class described in full here
http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

For yesterday you can try to simply subtract 1000 * 60 * 60 * 24 milliseconds from the date you want to get yesterday from and format that value:
Date yesterday = new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L));

Open in new window


That's the quick-and-dirty way, and, as noted in the comments, it might break when daylight savings transitions happen (two days per year). Recommended alternatives are the Calendar API, the Joda API or the new JDK 8 time API:
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);

Open in new window


adjust accordingly if you want to find yesterday from a certain date

org.apache.commons.lang3.time.DateUtils is more intuitive and easy to use

Date date = DateUtils.addDays(new Date(), -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);

Open in new window

will do the trick.
You can also create a Date() object for "yesterday":
private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

public final static String format(Date date) {
    DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
    return dateFormat.format(date);
}

public final static String formatToday() {
    return format(new Date());
}

public final static String formatYesterday() {
    return format(new Date(new Date().getTime() - 24*3600*1000));
}
 

Open in new window

0
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 400 total points
ID: 41767815
Should be easy:


Given you have:
 LocalDate now = LocalDate.now();

get first day of month by now.withDayOfMonth(1)
get week start date by now.with(WeekFields.of(Locale.getDefault()).getFirstDayOfWeek())
get yesterdays date by calling now.minusDays(1)

Each returns a LocalDate object since the LocalDate class is immutable.
0
 
LVL 1

Assisted Solution

by:srikotesh
srikotesh earned 0 total points
ID: 41768466
Hi gurpsbassi,

First day of week is printing nextweek first day

get first day of month by now.withDayOfMonth(1) //2016-08-01
get week start date by now.with(WeekFields.of(Locale.getDefault()).getFirstDayOfWeek()) //2016-08-28
get yesterdays date by calling now.minusDays(1) //2016-08-23
0
 
LVL 1

Author Comment

by:srikotesh
ID: 41768472
day of week should be currenweek firstday-->2016-08-21
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41768688
will try and get back to you later. In the middle of something.
You could try experimenting with it in the meantime.
1
 
LVL 1

Author Comment

by:srikotesh
ID: 41769143
I got day of week

LocalDate now = LocalDate.now();
            TemporalField fieldUS = WeekFields.of(Locale.US).dayOfWeek();
            System.out.println(now.with(fieldUS, 1)); // 2016-08-21 (Sunday)

//here i have used Locale.US
IS THIS CORRECT OR IT IS SPECIFIC ONE TIMEZONE
0
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41769364
Different cultures have different week start dates. Like USA has sundays.
Your application should know what locale you are in.
0
 
LVL 1

Author Closing Comment

by:srikotesh
ID: 41770527
Thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java virtual machine monitoring tools 3 39
array11 challenge 16 51
Systems talking to each other 5 108
Java and GPO 11 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now