How to convert a string to date or date?

5

Here are the statements I need to convert from string to date, can you help me?

ObjAl.setDataEmissao(TxtDataEmissao.getText());
ObjAl.setDataNascimento(TxtDataNascimento.getText());
    
asked by anonymous 13.01.2016 / 15:20

4 answers

4

Something like this:

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
java.sql.Date data = new java.sql.Date(format.parse(dataStr).getTime());

Remember to adapt to your need.

    
13.01.2016 / 15:24
4

An alternative would be to create a utility class with the methods that convert the dates in case you need to do conversions in more places. Or you can create the methods in the Class itself, Example:

    public static Date parseDate(String data) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        return sdf.parse(data);
    }

    public static String parseDate(Date data) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        return sdf.format(data);
    }

    public static java.sql.Date getSqlDate(Date data) {
        return new java.sql.Date(data.getTime());
    }   

Then just call these methods by passing a String or Date, like this:

//Converte String pra Date
ObjAl.setDataEmissao(parseDate(TxtDataEmissao.getText()));
    
13.01.2016 / 16:32
4

The SimpleDateFormat is intended to convert java.util.Date to String and vice versa.

When building this object, you can tell which date pattern you want to transform or retrieve.

If this pattern is not informed, the meadow will be used by Locale of User.

Follows the class documentation.

Example usage:

public static void main(String[] args) {
        Date dataAtual = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

        String DateToStr = format.format(dataAtual);
        System.out.println(DateToStr);

        format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
        DateToStr = format.format(dataAtual);
        System.out.println(DateToStr);

        format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
        DateToStr = format.format(dataAtual);
        System.out.println(DateToStr);

        format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
        DateToStr = format.format(dataAtual);
        System.out.println(DateToStr);

        try {
            Date strToDate = format.parse(DateToStr);
            System.out.println(strToDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

In your case, you should consider the format in which TxtDataEmissao is displaying to instantiate your SimpleDateFormat :

 SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
// Verifique o formato!

    try {       
               ObjAl.setDataEmissao(format.parse(TxtDataEmissao.getText()));
       } catch (ParseException e) {
            e.printStackTrace();
       }
    
13.01.2016 / 16:03
4

There is DateTimeFormatter .

Using it, you can convert a string to an LocalDate , which is part of the new Java Date API, an example:

String data = "13 Janeiro 2016";

DateTimeFormatter format = DateTimeFormatter.ofPattern("d MMMM yyyy");
LocalDate localDate = LocalDate.parse(data, format);

Accepted standards can be found at Javadoc , although they are nothing new to anyone who works with dates in Java.

Having localDate , we can manipulate the object to, for example, get the day of the week:

String diaDaSemana = localDate.getDayOfWeek()
                              .getDisplayName(TextStyle.FULL, Locale.getDefault()); 

System.out.println(diaDaSemana); // Quarta-feira

The new Java date API is inspired by Joda and its main goal is to work with dates and times.

Unlike an object Date that represents an instant in the timeline, stored in a long that counts the milliseconds since midnight of 1970.

If you really need to work with milliseconds, go from Date or Instant . But if you want to work simply with time objects, be cool and use the new api.

  • LocalDate is there to manipulate dates exclusively.
  • If you only want to work with time / time, there's LocalTime .
  • If you need the date and time, there's LocalDateTime .

Finally, you can transform LocalDateTime into a Date object through the Timestamp :

String data = "13 Janeiro 2016, 16:22";

DateTimeFormatter format = DateTimeFormatter.ofPattern("d MMMM yyyy, HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse(data, format); 

Date d = Timestamp.valueOf(localDateTime);
System.out.println(d); // 2016-01-13 16:22:00.0
    
13.01.2016 / 20:05