?
Solved

Getting Date into JTextField

Posted on 2004-04-01
9
Medium Priority
?
481 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
Technology Partners: 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 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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