Solved

java: processing date variable

Posted on 2008-10-15
4
830 Views
Last Modified: 2013-11-23
I want to create a method that takes in a date variable from the user.  Can someone show me how to do this in Java and then format it so that the final date saved is 10/16/2008.  thanks.
0
Comment
Question by:newbieal
  • 2
4 Comments
 
LVL 4

Author Comment

by:newbieal
ID: 22725443
I want something like this, however the date is supplied by the user:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");

System.out.println("Today is " + df.format(new Date()) );

Open in new window

0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 20 total points
ID: 22725449
You can use JCalender Bean for date input from http://www.toedter.com/
That saves you a lot of writing.

A Date is saved to a millisecond format.
The format you want is produced when you output only:

Date d;
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy");
String result = fmt.format(d);

;JOOP!
0
 
LVL 4

Author Comment

by:newbieal
ID: 22725549
I already have a method in place that passes a string variable that holds the date, but I just don't understand how to use that variable to process it using the code snippet above.
0
 
LVL 6

Accepted Solution

by:
javaexperto earned 30 total points
ID: 22726463
suppose the user call your method like this:
myMethod("12/25/2008");
public void myMethod(String date) {

    /*here you pass the string to the SimpleFomart constructor*/

    SimpleDateFormat df = new SimpleDateFormat(date);

    System.out.println("Today is " + df.format(new Date()) );

}

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

11 Experts available now in Live!

Get 1:1 Help Now