how to get yesterday date and time and today date and time in java8

Hi Experts,
how to get the yesterday date and time and today date and time(current time) using java8.
public static void main(String args[]){
		getYesterdayDateString();
		getTodayDateString();
	}
	private static String getYesterdayDateString() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);    
        System.out.println(dateFormat.format(cal.getTime()));
        return dateFormat.format(cal.getTime());
}

	private static String getTodayDateString() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormat.format(date));
        return dateFormat.format(date);
}
}

Open in new window


my above code is returning date in string format.
i want to know will i get yesterday date and today date with return type date , and with return type string
i mean the above code i am getting yesterday and today with return type string,
will i get the todaydate and yesterdate in both return types string as well date.

can some one suggest me how to do it.

Thanks
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
Try this.
Note: I've types this without a development environment, so may not compile.
But you will get the idea.

If you want to return LocalDateTime instead of String, don't bother with the toString at the end.

private static String getYesterdayDateString() {
        DateFormatter dateFormat = DateFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
        return dateFormat.format(cal.getTime());
}

    private static String getTodayDateString() {
        DateFormatter dateFormat = DateFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime today = LocalDateTime.now();
        return dateFormat.format(cal.getTime());
}

Open in new window

srikoteshAuthor Commented:
which one i have to try
gurpsbassiCommented:
try this
 private static String getYesterdayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
        return dateFormat.format(cal.getTime());
}

    private static String getTodayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime today = LocalDateTime.now();
        return dateFormat.format(cal.getTime());
}

Open in new window

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

srikoteshAuthor Commented:
i am getting the below output :
==========================
yesterday is 2015-07-06T21:08:50.688
today is 2015-07-07T21:08:50.690

how to remove T between date and time?
is there any way to get it in return type of date?
krakatoaCommented:
This is today :

Date today = new Date();

and this is yesterday:

Date yesterday = new Date(System.currentTimeMillis()-(24*60*60*1000));
gurpsbassiCommented:
Try this. It should get rid of the 'T'. There was a bug in the program.

private static String getYesterdayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
      return dateFormat.format(yesterday)
}

    private static String getTodayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime today = LocalDateTime.now();
        return dateFormat.format(today);
}

Open in new window

gurpsbassiCommented:
If you want to convert a LocalDateTime object into a legacy Date object all you do is:

final LocalDateTime today = LocalDateTime.now();
final Date d = Date.from(today.atZone(ZoneId.systemDefault()).toInstant());

Open in new window

private static String getYesterdayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
      return dateFormat.format(yesterday)
}

    private static String getTodayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime today = LocalDateTime.now();
        return dateFormat.format(today);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
Hi,
Hi Experts,

i have one more scenario need to get yesterday time and today time in long type

is it possible to get this yesterday and today time in long type in java8?
the below code is the good practice to do or is there any best alternative?

                Long startTime = System.currentTimeMillis();
		Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1);
		Long endTime = cal.getTimeInMillis();

Open in new window

krakatoaCommented:
Or

Long endTime = System.currentTimeMillis();
Long startTime = endTime-(24*60*60*1000);

Open in new window

gurpsbassiCommented:
is it possible to get this yesterday and today time in long type in java8?
the below code is the good practice to do or is there any best alternative?

Well, if you can get a java.util.Date object like I demonstrated above, there's no reason why you can't simply call getTime() on it to give you a millisecond value?
srikoteshAuthor Commented:
Excellent
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.