Solved

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

Posted on 2016-08-23
8
58 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 18

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 16

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 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 58
Error in @AspectJ Based AOP with Spring 2 18
spring jars download 1 35
Delete Android all or certain applications data programmatically 9 58
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 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