Solved

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

Posted on 2016-08-23
8
56 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 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Author Comment

by:srikotesh
ID: 41768472
day of week should be currenweek firstday-->2016-08-21
0
 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 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