R
R
roman38472014-05-17 18:53:23
Java
roman3847, 2014-05-17 18:53:23

How to work with date written in a string?

For example, I have records in the database: last name and date. The date is in the format: dd.mm.yyyy . Is it possible to somehow search by day or month separately? The search goes through the loop if(input_data.equals(current) -> display the record on the screen.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
svd71, 2014-05-18
@svd71

it all depends on how you are going to work with the data. You've specified the "Java" tag, but you're still talking about data. With a million rows and 20 columns in a table, your application will run very slowly if it doesn't drop dead.
conversion in Java code, you need to create a formatter that will then convert string data to date format.

SimpleDateFormat frm= new SimpleDateFormat("dd.mm.yyyy");
  String tst = "17.05.2014";
 
  try {
 
    Date date = frm.parse(tst);
    System.out.println(date);
    System.out.println(frm.format(date));
 
  } catch (ParseException e) {
    e.printStackTrace();
  }

in the database, you can specify sql-queries to limit the data received. For this, there are special functions for extracting values ​​from a date. For example from oracle-like databases:
SELECT EXTRACT(YEAR FROM DATE expDate) FROM Table1
where EXTRACT(MONTH FROM DATE expDate) = 2

E
Eugene, 2014-05-18
@zolt85

Store a date as a date, that's kind of useful. The JDBC driver will give you the date itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question