I need to get yesterday's date and format it YYYYMMDD
I know that you can use
to get the date, but how do I get yesterday's date and format it?Code:Date today = new Date();
Thanks!
Printable View
I need to get yesterday's date and format it YYYYMMDD
I know that you can use
to get the date, but how do I get yesterday's date and format it?Code:Date today = new Date();
Thanks!
import java.util.Date;
import java.text.*;
// Display todays date using a defualt format and the current
// local
// java.util.Date " a wrapper around a lond value the hold the
// number of milliseconds that have pass since midnight Janurary
// 1, 1970.
DateFormat defaultDate = DateFormat.getDateInstance();
System.out.println(defaultDate.format(new Date());
or
// display the current time using a short time format for the
// current local
DateFormat shortTime = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println(shortTimeFormat.format(new Date());
or
// use simple date format to define your own formatting template.
// java.text.SimpleDateFormat;
DateFormat myFormat = new SimpleDateFormat("yyyy.MM.dd");
System.out.println(myFormat.format(new Date());
just some formatting ideas....... im not to sure how to get yesterdays date though.
Ugly, but it works...
Code:/** You will need to add this to your import statements **/
import java.util.*;
import java.util.Date;
/** Here are the Variables **/
java.util.Calendar gc;
java.util.Date curDate;
String strCurDate = null;
String fileDate = new String();
/** Here is the code **/
gc = GregorianCalendar.getInstance ( ); // Gets the current Calendar instance
curDate = gc.getTime ( ); // Gets the Date time part of gc
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyyMMdd" ); //sets up the correct format for the date
long msCDate = curDate.getTime(); //Transforms the current date into miliseconds
long msYDate = msCDate - 86400000; // Subtracts off one days worth of miliseconds
java.util.Date yestDate = new java.util.Date (msYDate); //Transforms the miliseconds into a correect date
fileDate = sdf2.format ( yestDate ); //Changes the date into the correct date format