How to insert a date variable in java?

Java Date.....................?

  • this might sound a little silly.. how do you take date input in java list of my attempts 1.i used the Calender class Calendar c= Calendar.getInstance(); then i took individual inputs Calendar.YEAR.....MONTH DATE despite taking individual inputs...(except for DATE) i get wrong values when i print the values to the console is the some way in which i can take the complete input from the user eg 21/02/2000 no matter what i try ... i just cant seem to print the values i took as input to the console... this is what i tried... //////////////////////////////////////… System.out.println("Enter Year"); d.set(Calendar.YEAR, Integer.parseInt(buf.readLine())); System.out.println("Enter Month"); d.set(Calendar.MONTH, Integer.parseInt(buf.readLine())); System.out.println("Enter Date"); d.set(Calendar.DATE, Integer.parseInt(buf.readLine())); //////////////////////////////////////… values i gave as input year=2000, month=02,date =21 output year=5, month=3,date =1 ------>strange!! or any technique to take a date input would do.....

  • Answer:

    public static void main( String[] args) { int year, month, date; Scanner console = new Scanner( System.in); System.out.print( "Enter Year: "); year = console.nextInt(); System.out.print( "Enter Month: "); month = console.nextInt(); System.out.print( "Enter Day: "); date = console.nextInt(); Calendar calendar = Calendar.getInstance(); calendar.set( year, month-1, date); DateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss aaa"); System.out.println( dateFormat.format( calendar.getTime())); } good luck don't forget to choose best answer

Cherry at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.