Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-08-23
8
Medium Priority
?
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 18

Assisted Solution

by:xtermie
xtermie earned 400 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 16

Accepted Solution

by:
gurpsbassi earned 1600 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 2

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:srikotesh
ID: 41768472
day of week should be currenweek firstday-->2016-08-21
0
 
LVL 16

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 2

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 16

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 2

Author Closing Comment

by:srikotesh
ID: 41770527
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

609 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