Solved

Getting Date into JTextField

Posted on 2004-04-01
9
478 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

825 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