Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting Date into JTextField

Posted on 2004-04-01
9
Medium Priority
?
484 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

598 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