DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2004
    Location
    Finland
    Posts
    10

    Today(YES), Tomorrow(No),Yesterday(NO). Date releated problem

    If only someone could help

    All code(s) below gives me a current date,
    but I also need to get tomorrows and yesterdays dates.

    I think I canīt use simpledateformat because I need to import import java.sql.*;??????

    So if anyone can tell me how to modify some of the codes below, It would be great. All I need is that I get forexample yesterdays or tomorrows date with the
    "codes" below:


    these are the codes what I have try to use without success(they all give me current date)

    1.
    Code:
    java.sql.Date date = 
            new java.sql.Date((new GregorianCalendar()).getTime().getTime()); 
        jTextField2.setText(""+date);
    2.
    Code:
    Calendar cal = GregorianCalendar.getInstance();
          DateFormat fmt = DateFormat.getInstance();
          String buf = fmt.format(cal.getTime());
          
             
           jTextField2.setText(""+buf);
    3.
    Code:
     java.util.Date today =
    new java.util.Date();
    java.sql.Date sqlToday =
    new java.sql.Date(today.getTime()+1);
    jTextField2.setText(""+sqlToday);

  2. #2
    Join Date
    May 2004
    Posts
    219
    Code:
    cal.set(cal.DAY_OF_YEAR, cal.get(cal.DAY_OF_YEAR)-1);
    There are many other ways to do it...

  3. #3
    Join Date
    Aug 2004
    Location
    Finland
    Posts
    10

    problem solved

    And this one works

    // get a calendar set to the current date/time
    GregorianCalendar cal = new GregorianCalendar();

    // create a formatter for converting dates to day/month/year
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    String today = formatter.format(cal.getTime());
    cal.add(Calendar.DATE, -1);
    String yesterday = formatter.format(cal.getTime());
    cal.add(Calendar.DATE, 2);
    String tomorrow = formatter.format(cal.getTime());

    JTextField todayField = new JTextField(today);
    JTextField yesterdayField = new JTextField(yesterday);
    JTextField tomorrowField = new JTextField(tomorrow);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links