Solved

Getting Date into JTextField

Posted on 2004-04-01
9
476 Views
Last Modified: 2012-08-14
Hi,

I am trying to fetch date from the database, which is a date type there. I want to pouplate JTextField with the date in mm/dd/yy format. I mean set the text of the textfield to e.g., 04/01/04. Date can be any date not only the sysdate.
And also, suppose the date is 04/01/04. How can I select the next date i mean the date of next day..????? I wud appreciate for a quick response.

Thanx...!!!!

0
Comment
Question by:raaz007
  • 5
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10735870
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
testField.setText(sdf.format(yourDate));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10735883
If you want the next day:

Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DATE, 1);
textField.setText(sdf.format(cal.getTime()));
0
 

Expert Comment

by:RobCSP
ID: 10739455
Use the CEHJ Comment and later if you want to change the format of your date use this method:


 /**   Turns a date of a format other one
     * @return A chain with the date in the requested format
     * @param strFecha Date of entry
     * @param strFormatoIn Format of entry
     * @param strFormatoOut Format of exit
     * @throws Exception
     */
    public static String fechaEnFormato(String strFecha, String strFormatoIn, String strFormatoOut) throws Exception {
        String strFechaEnFormato = null;
        try {
           
            SimpleDateFormat formatterIn = new SimpleDateFormat(strFormatoIn);
            SimpleDateFormat formatterOut = new SimpleDateFormat(strFormatoOut);
            ParsePosition pos = new ParsePosition(0);
            strFechaEnFormato = (formatterOut.format(formatterIn.parse(strFecha, pos)));
        } catch (Exception objException) {
            throw (new Exception("Incorrect date format strFecha=" + strFecha + ",strFormatoOut=" + strFormatoOut));
        }
        return strFechaEnFormato;
    }


/*
I hope that it helps,
ROB*/
0
 

Author Comment

by:raaz007
ID: 10742730
I am using in the way you have mentioned ...

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
testField.setText(sdf.format(yourDate));


"your date"...i m getting from the database with timestamp but it throwing the following exception when i m executing the program...


java.lang.IllegalArgumentException: Cannot format given Object as a Date

      at java.text.DateFormat.format(DateFormat.java:279)

      at java.text.Format.format(Format.java:133)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 10742774
>>i m getting from the database with timestamp

In that case, that should be

Timestamp t = resultSet.getTimestamp("yourDateField");
java.util.Date yourDate = new java.util.Date(t.getTime());
// previous code should follow
0
 

Author Comment

by:raaz007
ID: 10742886
Date from the database is "2004-04-02 00:00:00.0" format..>!!!!!!
0
 

Author Comment

by:raaz007
ID: 10743089
Thanx a lot, now it worked fine....!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10743107
No - a date in a database doesn't have a format - it's just a number. What you're looking at is a particular *view* of it and a that's a view of a sql date type (no time portion). The formatting will come when you apply 'sdf'. In this case, since there is not time portion, you'll get

04/02/04
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10743120
8-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now