Results 1 to 3 of 3

Thread: Dates and formatting them...

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Dates and formatting them...

    I need to get yesterday's date and format it YYYYMMDD

    I know that you can use
    Code:
    Date today = new Date();
    to get the date, but how do I get yesterday's date and format it?

    Thanks!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  3. #3

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Talking

    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
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width