Languages
[Edit]
PL

Java - jak sformatowa─ç aktualn─ů dat─Ö i godzin─Ö za pomoc─ů wzorca milisekund np .: yyyy-MM-dd HH: mm: ss.milliseconds

3 points
Created by:
Sylwia
1337

1. Om├│wienie

W javie mo┼╝emy wy┼Ťwietla─ç aktualn─ů dat─Ö i godzin─Ö w milisekundach, gdy u┼╝ywamy wzorca SSS. Np. :

yyyy-MM-dd HH:mm:ss.SSS

2. LocalDateTime - aktualny czas w milisekundach - Java 8

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Example1 {

    public static void main(String[] args) {

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        LocalDateTime localDateTime = LocalDateTime.now();

        String format = fmt.format(localDateTime);
        System.out.println(format); // 2019-10-13 12:56:53.220
    }
}

Wynik:

2019-10-13 12:56:53.220

3. ZonedDateTime - aktualny czas w milisekundach - Java 8

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Example2 {

    public static void main(String[] args) {

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS Z");
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        String format = fmt.format(zonedDateTime);
        System.out.println(format); // 2019-10-13 13:07:10.912 +0200
    }
}

Wynik:

2019-10-13 13:07:10.912 +0200

4. Data - aktualny czas w milisekundach

import java.text.SimpleDateFormat;
import java.util.Date;

public class Example3 {

    public static void main(String[] args) {

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date();

        String format = fmt.format(date);
        System.out.println(format); // 2019-10-13 12:57:05.053
    }
}

Wynik:

2019-10-13 12:57:05.053

5. Kalendarz - aktualny czas w milisekundach

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Example4 {

    public static void main(String[] args) {

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Calendar calendar = Calendar.getInstance();

        String format = fmt.format(calendar.getTime());
        System.out.println(format); // 2019-10-13 12:57:14.897
    }
}

Wynik:

2019-10-13 12:57:14.897

 

Po┼é─ůczone pytania:

  1. Java - wy┼Ťwietl┬áaktualny czas w milisekundach

Biblografia:

  1. DateTimeFormatter - JavaDoc
  2. LocalDateTime - JavaDoc
  3. Date - JavaDoc
  4. SimpleDateFormat - JavaDoc
  5. Calendar - JavaDoc
  6. ZonedDateTime - JavaDoc

 

 

     

    Hey ­čĹő
    Would you like to know what we do?
    • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
    • We welcome everyone,
      no matter what the experience,
      no matter how basic the question is,
      this community will help you.