Wednesday, January 4, 2017

How to get Yester day Date


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.javasunderesan;

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

public class FindYesterDayDate {

 public static void main(String[] args) {
  FindYesterDayDate fydd = new FindYesterDayDate();
  System.out.println(fydd.getYesterdayDateString());
 }

 public String getYesterdayDateString() {
  String prevDate = null;
  Calendar cal = null;
  SimpleDateFormat datetimeFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
  String currDate = datetimeFormatYYYYMMDD.format(new Date());
  try {
   cal = Calendar.getInstance();
   cal.setTime(datetimeFormatYYYYMMDD.parse(currDate));
   cal.add(Calendar.DATE, -1);
   prevDate = datetimeFormatYYYYMMDD.format(cal.getTime());
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   cal = null;
  }
  return prevDate;
 }
}

No comments :

Post a Comment