Solved

Getting Date into JTextField

Posted on 2004-04-01
9
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

691 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