How to get and format the current date and time?

0

I would like to know how to get the current date and time and then format them in JAVA.

For example, I have these two values:

23062017 
212010

I want to store each value in a variable, and then I want to format it like this:

23/06/2017 
21:20:10
    
asked by anonymous 23.06.2017 / 17:16

4 answers

4

To recover the current system date and time you can do:

Date dataHoraAtual = new Date();
String data = new SimpleDateFormat("dd/MM/yyyy").format(dataHoraAtual);
String hora = new SimpleDateFormat("HH:mm:ss").format(dataHoraAtual);

In this way you will receive the date and time in the formats you want and in separate variables.

For reference, #

  •     
  • 23.06.2017 / 18:34
    1

    The title of the question mentions "current date and time", but in the body of the question specific values are mentioned. Anyway, there is a solution for both.

    The other responses use SimpleDateFormat , but from Java 8, you can use the API% com_of% . In this API, there are classes to manipulate just the date, or just the time, which seems to be what you need.

    To read the java.time mentioned, you can use Strings , which turns java.time.format.DateTimeFormatter into the corresponding type ( String for dates, or java.time.LocalDate for hours).

    Then, I use another java.time.LocalTime to format them (convert to another format):

    // converter 23062017 para 23/06/2017
    DateTimeFormatter parserData = DateTimeFormatter.ofPattern("ddMMuuuu");
    LocalDate data = LocalDate.parse("23062017", parserData);
    DateTimeFormatter formatterData = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    String dataFormatada = formatterData.format(data); // 23/06/2017
    
    // converter 212010 para 21:20:10
    DateTimeFormatter parserHora= DateTimeFormatter.ofPattern("HHmmss");
    LocalTime hora = LocalTime.parse("212010", parserHora);
    DateTimeFormatter formatterHora = DateTimeFormatter.ofPattern("HH:mm:ss");
    String horaFormatada = formatterHora.format(hora); // 21:20:10
    

    If you want to use the current date and time, you can use a DateTimeFormatter (which already has both information) and format it:

    // data/hora atual
    LocalDateTime agora = LocalDateTime.now();
    
    // formatar a data
    DateTimeFormatter formatterData = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    String dataFormatada = formatterData.format(agora);
    
    // formatar a hora
    DateTimeFormatter formatterHora = DateTimeFormatter.ofPattern("HH:mm:ss");
    String horaFormatada = formatterHora.format(agora);
    

    If you do not already use Java 8, you can use ThreeTen Backport , which has the same classes already mentioned and works basically the same way. The difference is that they are in the package LocalDateTime (instead of org.threeten.bp ).

    The backport is compatible with JDK 6 and 7.

    If you can use this API instead of java.time and Date , do so. The new API is far superior and fixes several existing problems in the old API .

        
    21.06.2018 / 21:00
    0
    public static void main(String[] args) {
    
        String dataAtualFormatada = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
                                        .format(System.currentTimeMillis());
    
        //Seta valores da data
        String dataString = dataAtualFormatada.substring(0,10);
    
        //Seta valores do horário
        String horario = dataAtualFormatada.substring(11,19);     
    
    }
    
        
    23.06.2017 / 18:58
    0

    In this example it generates two day and time strings, with the format for 02-03-2018 and string time 22:21:30

    Calendar calendar = Calendar.getInstance();//cria o obj calendar e atribui a hora e data do sistema
    Date data = calendar.getTime();//transforma o obj calendar em obj Date
    
    SimpleDateFormat sddia = new SimpleDateFormat("dd-MM-yyyy");//cria um obj de formatação de data
    SimpleDateFormat sdhora = new SimpleDateFormat("HH:mm:ss");//cria um obj de formatação de hora
    String dia = sddia.format(data);//gera a string final formatada no estilo "dd-MM-yyyy"
    String hora = sdhora.format(data);//gera a string final formatada no estilo "HH:mm:ss"
    
        
    03.03.2018 / 02:40