Solved

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

Posted on 2016-08-23
8
64 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 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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