Search code examples
javadateintegerlong-integerjava.util.date

Is it possible to turn java.util.Date() to int?


I am developing an application where I have a JFormattedTextField with the current date in yyyy/mm/dd format and I want to move to int only these values. But the problem and what the eu am doing is returning more values

JFormattedTextField textId = new JFormattedTextField(new SimpleDateFormat("yyyy/MM/dd"));
textId.setValue(new java.util.Date());
textId.setBounds(20, 310, 100, 25);
Date data = (Date)textId.getValue();
long dataFinal = data.getTime();

How can I make the value of the Int only YYYY/MM/DD? With long, the variable dataFinal at the moment gives 1603883824076 is this the current time in nanoseconds?


Solution

  • Do you mean something like the following?

    JFormattedTextField textId = new JFormattedTextField(new SimpleDateFormat("yyyy/MM/dd"));
    textId.setValue(new java.util.Date());
    textId.setBounds(20, 310, 100, 25);
    String str = textId.getText().replaceAll("/", "");
    int integer = Integer.parseInt(str);
    

    The value of integer is 20201028 (because today's date is 2020/10/28, i.e. 28th October 2020)