Languages
[Edit]
DE

Java - wie formatiert man die aktuellen Datums- und Uhrzeitangabe mit einem Milisekundenmuster, z. B. yyyy-MM-dd HH:mm:ss.miliseconds

3 points
Created by:
Nikki
10520

1. Übersicht

In Java kann man mit hilfe eines SSS - Musters, die aktuellen Datums- und Uhrzeitangaben mit einem Milisekundenmuster anzeigen.

Zum Beispiel:

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

2. LocalDateTime - aktuelle Zeit mit Milisekunden - 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
    }
}

Ausgabe:

2019-10-13 12:56:53.220

3. ZonedDateTime - aktuelle Zeit mit Milisekunden - 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
    }
}

Ausgabe:

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

4. Datum - aktuelle Zeit mit Milisekunden

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
    }
}

Ausgabe:

2019-10-13 12:57:05.053

5. Kalender - aktuelle Zeit mit Milisekunden

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
    }
}

Ausgabe:

2019-10-13 12:57:14.897

Zusammengeführte Fragen

  1. Java - aktuelle Uhrzeit in Milisekunden anzeigen

Literaturverzeichnis

  1. DateTimeFormatter - JavaDoc
  2. LocalDateTime - JavaDoc
  3. Date - JavaDoc
  4. SimpleDateFormat - JavaDoc
  5. Calendar - JavaDoc
  6. ZonedDateTime - JavaDoc
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join