Java Yesterday getYesterdayDateAsString()

Here you can find the source of getYesterdayDateAsString()

Description

get the yesterday's date as string using the mysql date format yyyy-MM-dd

License

Mozilla Public License

Declaration

public static String getYesterdayDateAsString() 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  .j av  a 2 s.  c o m*/
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final String mysqlDateFormat = "yyyy-MM-dd";

    /** 
     * get the yesterday's date as string using the mysql date format yyyy-MM-dd 
     **/
    public static String getYesterdayDateAsString() {
        // get a calendar instance, which defaults to "now"
        Calendar calendar = Calendar.getInstance();

        // add one day to the date/calendar
        calendar.add(Calendar.DAY_OF_YEAR, -1);

        // now get "yesterday"
        Date yesterday = calendar.getTime();

        // get a SimpleDateFormat instance, passing the mysql date format as parameter
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(mysqlDateFormat);

        // return date as string
        return simpleDateFormat.format(yesterday);
    }
}

Related

  1. getYesterdayBegin()
  2. getYesterdayCompactTime()
  3. getYesterdayDate(Date date)
  4. getYesterdayDate(int day)
  5. getYesterdayDate(String format)
  6. getYesterdayFormat(String format)
  7. getYesterdayFormatString(String format)
  8. getYesterdaysDate()
  9. getYesterdayStartDate()