How to set date as input in java?

Is there any direct way to set a date for a variable, but as an input? I mean that I do not know the date during development, the user must provide it. I tried the following code, but it does not work: Calendar myDate = new GregorianCalendar (int year, int month, int day);

+4
source share
5 answers

Try using the following code. I process the input string to create a date

// To take the input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Date ");

String date = scanner.next();

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date2=null;
try {
    //Parsing the String
    date2 = dateFormat.parse(date);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(date2);
+8
source

TL; dr

 LocalDate.of( 2026 , 1 , 23 )  // Pass: ( year , month , day )

java.time

, , , , java.time.

LocalDate

LocalDate.

LocalDate ld = LocalDate.of( 2026 , 1 , 23 );

, : int Java?

int y = Integer.parseInt( yearInput );
int m = Integer.parseInt( monthInput );  // 1-12 for January-December.
int d = Integer.parseInt( dayInput );

LocalDate ld = LocalDate.of( y , m , d );

Table of date-time types in Java, both modern and legacy.


java.time

java.time Java 8 . , java.util.Date, Calendar, SimpleDateFormat.

Joda-Time, , java.time.

, Oracle Tutorial. . JSR 310.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+2

@SK08 , , .

    Scanner scanner = new Scanner(System.in);
    String str[] = {"year", "month", "day" };
    String date = "";

    for(int i=0; i<3; i++) {
        System.out.println("Enter " + str[i] + ": ");
        date = date + scanner.next() + "/";
    }
    date = date.substring(0, date.length()-1);
    System.out.println("date: "+ date); 

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    Date parsedDate = null;

    try {
        parsedDate = dateFormat.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return parsedDate;
+1

, , setlenient -

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Datinput {

    public static void main(String args[]) {
        int n;
        ArrayList<String> al = new ArrayList<String>();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        String da[] = new String[n];
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        Date date[] = new Date[n];
        in.nextLine();
        for (int i = 0; i < da.length; i++) {
            da[i] = in.nextLine();
        }
        for (int i = 0; i < da.length; i++) {

            try {
                date[i] = sdf.parse(da[i]);
            } catch (ParseException e) {

                e.printStackTrace();
            }
        }

        in.close();
    }
}
0

, !

package javaapplication2;
//@author Ibrahim Yesilay
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class JavaApplication2 {  
    public static void main(String[] args) throws ParseException {
    Scanner giris = new Scanner(System.in);        
        System.out.println("gΓΌn:");
        int d = giris.nextInt();
        System.out.println("ay:");
        int m = giris.nextInt();
        System.out.println("yil:");
        int y = giris.nextInt();
        String tarih;
        tarih = Integer.toString(d) + "/" + Integer.toString(m) + "/" + Integer.toString(y);  
        System.out.println("Tarih : " + tarih); 
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date girilentarih = null;
        girilentarih = dateFormat.parse(tarih);
        System.out.println(dateFormat.format(girilentarih));      
    }   
}
0
source

Source: https://habr.com/ru/post/1568450/


All Articles