0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Just format date using SimpleDateFormater as "yyyyMMdd" and parse this to int value. – MUNGAI NJOROGE Oct 28 '20 at 11:27
  • Read this: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html – Koenigsberg Oct 28 '20 at 11:27
  • I'm fine with doing any reading at all, honestly. To find out *e.g.* about format being milliseconds. – Koenigsberg Oct 28 '20 at 11:41
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 29 '20 at 04:11
  • Why do you want an `int`? What is the need? – Ole V.V. Oct 29 '20 at 04:21

2 Answers2

1

Two suggestions:

  1. Use java.time, the modern Java date and time API, for your date work.
  2. If you need an int, use a count of days since the epoch day of January 1, 1970.

The two suggestions go nicely hand in hand since java.time offers a toEpochDay method for the conversion:

    Format localDateFormat = DateTimeFormatter.ofPattern("uuuu/MM/dd")
                                .toFormat(LocalDate::from);
    JFormattedTextField textId = new JFormattedTextField(localDateFormat);
    textId.setValue(LocalDate.now(ZoneId.systemDefault()));
    textId.setBounds(20, 310, 100, 25);
    LocalDate data = (LocalDate) textId.getValue();
    int dataFinal = Math.toIntExact(data.toEpochDay());

Today — October 29, 2020 — will give you 18564.

… with long the variable datefinal at the moment gives 1603883824076 is this the current time in nanoseconds? …

They are milliseconds since the epoch of January 1, 1970, 00:00 UTC. And as you have probably noticed, they don’t fit into an int.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • My answer to the question how to make a jtextfield having a fixed date format? giving more detail on using LocalDate with JFormattedTextField.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

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)

Abra
  • 19,142
  • 7
  • 29
  • 41