Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:10:48+00:00 2026-06-07T10:10:48+00:00

The following function produces today’s date; how can I make it produce only yesterday’s

  • 0

The following function produces today’s date; how can I make it produce only yesterday’s date?

private String toDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();    
        return dateFormat.format(date).toString();
}

This is the output:

2012-07-10

I only need yesterday’s date like below. Is it possible to do this in my function?

2012-07-09
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:10:50+00:00Added an answer on June 7, 2026 at 10:10 am

    Update

    There has been recent improvements in datetime API with JSR-310.

    Instant now = Instant.now();
    Instant yesterday = now.minus(1, ChronoUnit.DAYS);
    System.out.println(now);
    System.out.println(yesterday);
    

    https://ideone.com/91M1eU

    Outdated answer

    You are subtracting the wrong number:

    Use Calendar instead:

    private Date yesterday() {
        final Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }
    

    Then, modify your method to the following:

    private String getYesterdayDateString() {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            return dateFormat.format(yesterday());
    }
    

    See

    • IDEOne Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: Private Function XMLEncode(ByVal s As String) As String Return
The following code produces the error, Templates can be used only with field access,
Help me translate following block of the Haskell code. The run function produces text
Morning- I need a function that would produce the following kind of looking sequence:
I have the following function which produces a runtime error 52 (bad filename or
I have the following function that produces numbered input fields with id s and
Suppose I have the following function, the value it produces is needed by many
The following script produces Hello, undefined, Hello message boxes: function action(callback) { window.setTimeout(callback, 1000);
The following racket function produces the error: reference to undefined identifier: val This is
Can anyone tell me why the following code function s3() function ret = fun(x)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.