Inhaltsverzeichnis
Syntax of Java LocalDateTime
Methods of LocalDateTime in Java
Examples of Java LocalDateTime
Example #5
Conclusion
Heim Java javaLernprogramm Java LocalDateTime

Java LocalDateTime

Aug 30, 2024 pm 03:52 PM
java

LocalDateTime in java displays the local date and time on the output screen. The default format for displaying the time is YYYY-MM-DD-hh-mm-ss-zzz. The different factors in showing the date and time are the year, month, day, hours, minutes, seconds, and nanoseconds. The date and time can be added to a particular number of days, subtracted by a number of days, and finally, the output can be produced very smoothly. The LocalDateTime class is a final class; hence, no extension of the class is allowed. LocalDateTime class is a value-based class used to check whether two dates and times are equal to each other or not with equals().

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Java LocalDateTime

The Java LocalDateTime class is a part of java.time package. We can create the java instance of the class in the following manner.

The following is the syntax.

import java.time.LocalDateTime;
Nach dem Login kopieren

We can also pass values to the of() in the LocalDateTime class.

The syntax of the following is given below:

LocalDateTime Idt= LocalDateTime.of(2011,15,6,6,30,50,100000);
Nach dem Login kopieren

We can also use the parse() and pass the values of the time in String representation.

LocalDateTime Idt= LocalDateTime.parse("2011-11-10T22:11:03.46045");
Nach dem Login kopieren

Also, we can use ofInstant() by passing ID and Zone ID information.

LocalDateTime Idt= LocalDateTime.ofInstant(Instant.now(), ZoneId.SystemDefault());
Nach dem Login kopieren

Methods of LocalDateTime in Java

There are different methods in the Java LocalDateTime class.

  • String format(DateTimeFormatter formatter): Used to format the date and time using the specified formatter.
  • LocalDateTime minusDays(long days): Used to format the date and time using specific days, which are to be subtracted from the respective date.
  • LocalDateTime plusDays(long days): Used to add a certain number of days to the current date and then print the output respectively.
  • int get(TemporalField field): Used to get the date and time values as an integer format that is int.
  • static LocalDateTime now(): We use this method to retrieve the default date and time from the current time zone, which serves as the default time zone.

Examples of Java LocalDateTime

Given below are the examples mentioned:

Example #1

In the first coding example, we will witness the format() used in java to format a specific amount of code.

Code:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample
{
public static void main(String[] args)
{
LocalDateTime now = LocalDateTime.now();
System.out.println("Before doing Formatting: " + now);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String fDTime = now.format(format);
System.out.println("After doing Formatting: " + fDTime);
}
}
Nach dem Login kopieren

Output:

As shown in the sample output, we can see that the date and time before and after formatting have been shown.

Java LocalDateTime

Example #2

In the second program, we will see the working of the minusDays() when a certain date subtracts up certain values of days from the date and then finally prints the specific output.

Code:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample2
{
public static void main(String[] args)
{
LocalDateTime dt1 = LocalDateTime.of(2018, 2, 14, 15, 22);
LocalDateTime dt2 = dt1.minusDays(100);
System.out.println("Before Formatting: " + dt2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String fDTime = dt2.format(format);
System.out.println("After Formatting: " + fDTime );
}
}
Nach dem Login kopieren

In the above code, we subtract a certain number of days; in this case, it is 100 from the date 14th February 2018, as given in the code. We get the answer before formatting as well as after formatting, which is shown below.

Output:

Java LocalDateTime

Example #3

The plusDays() function is highly similar to the minusDays() function, with the only distinction being that it adds days to the current date instead of subtracting a specific number of days. So in this coding example, we will add a certain number of days to the existing date and time.

Code:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample3
{
public static void main(String[] args) {
LocalDateTime dt1 = LocalDateTime.of(2018, 1, 8, 12, 34);
LocalDateTime dt2 = dt1.plusDays(60);
System.out.println("Before Formatting: " + dt2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String fDTime = dt2.format(format);
System.out.println("After Formatting: " + fDTime );
}
}
Nach dem Login kopieren

In this sample code, we give the date as 8th January 2018 in the default date provided. And we see the added days, that is, 60 days. When we add 60 days to the code, we see that the date has changed, and it has been 9th March 2018. Time has remained the same. Only the number of days has changed, moving the date from 8th January to 9th March.

Output:

Java LocalDateTime

Example #4

In this coding example, we will see the features of the get(), where we will get the integer values of the date and time, respectively. We will also see a coding example to illustrate the program properly.

Code:

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LocalDateTimeExample4
{
public static void main(String[] args)
{
LocalDateTime b = LocalDateTime.of(2018, 3, 10, 14, 36);
System.out.println(b.get(ChronoField.DAY_OF_WEEK));
System.out.println(b.get(ChronoField.DAY_OF_YEAR));
System.out.println(b.get(ChronoField.DAY_OF_MONTH));
System.out.println(b.get(ChronoField.HOUR_OF_DAY));
System.out.println(b.get(ChronoField.MINUTE_OF_DAY));
}
}
Nach dem Login kopieren

The sample code gives the Day of the Week, Day of the Year, Day of the Month, Hour of the day, and Minute of the day in chronological order. The program calls the ChronoField package to ensure that it runs correctly and produces the specific output as required.

Output:

Java LocalDateTime

Example #5

In this coding example, we will see the now() in the LocalDateTime class in Java programming language. This program will return the current date and time along with the seconds in the program.

Code:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample5
{
public static void main(String[] args)
{
LocalDateTime dt1 = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String fDTime = dt1.format(format);
System.out.println(fDTime);
}
}
Nach dem Login kopieren

Output:

The output clearly displays the date and time of the current time zone in the format of hh-mm-ss, providing a clear representation.

Java LocalDateTime

Conclusion

This article has seen several programs illustrating all the methods and functions inside the LocalDateTime class. Also, we know the syntax of the LocalDateTime class that is present. Beyond this, we also notice the output of several programs. In aeronautical engineering, professionals frequently utilize the LocalDateTime class to maintain and monitor an aircraft’s current date and time, observing any changes in time zones and their impact on the watch time.

Das obige ist der detaillierte Inhalt vonJava LocalDateTime. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

<🎜>: Bubble Gum Simulator Infinity - So erhalten und verwenden Sie Royal Keys
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusionssystem, erklärt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Flüstern des Hexenbaum
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Java-Tutorial
1672
14
PHP-Tutorial
1276
29
C#-Tutorial
1256
24
PHP vs. Python: Verständnis der Unterschiede PHP vs. Python: Verständnis der Unterschiede Apr 11, 2025 am 12:15 AM

PHP und Python haben jeweils ihre eigenen Vorteile, und die Wahl sollte auf Projektanforderungen beruhen. 1.PHP eignet sich für die Webentwicklung mit einfacher Syntax und hoher Ausführungseffizienz. 2. Python eignet sich für Datenwissenschaft und maschinelles Lernen mit präziser Syntax und reichhaltigen Bibliotheken.

PHP: Eine Schlüsselsprache für die Webentwicklung PHP: Eine Schlüsselsprache für die Webentwicklung Apr 13, 2025 am 12:08 AM

PHP ist eine Skriptsprache, die auf der Serverseite weit verbreitet ist und insbesondere für die Webentwicklung geeignet ist. 1.PHP kann HTML einbetten, HTTP -Anforderungen und Antworten verarbeiten und eine Vielzahl von Datenbanken unterstützt. 2.PHP wird verwendet, um dynamische Webinhalte, Prozessformdaten, Zugriffsdatenbanken usw. mit starker Community -Unterstützung und Open -Source -Ressourcen zu generieren. 3. PHP ist eine interpretierte Sprache, und der Ausführungsprozess umfasst lexikalische Analyse, grammatikalische Analyse, Zusammenstellung und Ausführung. 4.PHP kann mit MySQL für erweiterte Anwendungen wie Benutzerregistrierungssysteme kombiniert werden. 5. Beim Debuggen von PHP können Sie Funktionen wie error_reporting () und var_dump () verwenden. 6. Optimieren Sie den PHP-Code, um Caching-Mechanismen zu verwenden, Datenbankabfragen zu optimieren und integrierte Funktionen zu verwenden. 7

Brechen oder aus Java 8 Stream foreach zurückkehren? Brechen oder aus Java 8 Stream foreach zurückkehren? Feb 07, 2025 pm 12:09 PM

Java 8 führt die Stream -API ein und bietet eine leistungsstarke und ausdrucksstarke Möglichkeit, Datensammlungen zu verarbeiten. Eine häufige Frage bei der Verwendung von Stream lautet jedoch: Wie kann man von einem Foreach -Betrieb brechen oder zurückkehren? Herkömmliche Schleifen ermöglichen eine frühzeitige Unterbrechung oder Rückkehr, aber die Stream's foreach -Methode unterstützt diese Methode nicht direkt. In diesem Artikel werden die Gründe erläutert und alternative Methoden zur Implementierung vorzeitiger Beendigung in Strahlverarbeitungssystemen erforscht. Weitere Lektüre: Java Stream API -Verbesserungen Stream foreach verstehen Die Foreach -Methode ist ein Terminalbetrieb, der einen Vorgang für jedes Element im Stream ausführt. Seine Designabsicht ist

Php gegen andere Sprachen: Ein Vergleich Php gegen andere Sprachen: Ein Vergleich Apr 13, 2025 am 12:19 AM

PHP eignet sich für die Webentwicklung, insbesondere für die schnelle Entwicklung und Verarbeitung dynamischer Inhalte, ist jedoch nicht gut in Anwendungen auf Datenwissenschaft und Unternehmensebene. Im Vergleich zu Python hat PHP mehr Vorteile in der Webentwicklung, ist aber nicht so gut wie Python im Bereich der Datenwissenschaft. Im Vergleich zu Java wird PHP in Anwendungen auf Unternehmensebene schlechter, ist jedoch flexibler in der Webentwicklung. Im Vergleich zu JavaScript ist PHP in der Back-End-Entwicklung präziser, ist jedoch in der Front-End-Entwicklung nicht so gut wie JavaScript.

PHP vs. Python: Kernmerkmale und Funktionen PHP vs. Python: Kernmerkmale und Funktionen Apr 13, 2025 am 12:16 AM

PHP und Python haben jeweils ihre eigenen Vorteile und eignen sich für verschiedene Szenarien. 1.PHP ist für die Webentwicklung geeignet und bietet integrierte Webserver und reichhaltige Funktionsbibliotheken. 2. Python eignet sich für Datenwissenschaft und maschinelles Lernen mit prägnanter Syntax und einer leistungsstarken Standardbibliothek. Bei der Auswahl sollte anhand der Projektanforderungen festgelegt werden.

Auswirkungen von PHP: Webentwicklung und darüber hinaus Auswirkungen von PHP: Webentwicklung und darüber hinaus Apr 18, 2025 am 12:10 AM

PhPhas significantantyPactedWebDevelopmentAndendendsbeyondit.1) iTpowersMAjorPlatforms-LikewordpressandExcelsInDatabaseInteractions.2) php'SadaptabilityAllowStoscaleForLargeApplicationsfraMe-Linien-Linien-Linien-Linienkripte

PHP: Die Grundlage vieler Websites PHP: Die Grundlage vieler Websites Apr 13, 2025 am 12:07 AM

Die Gründe, warum PHP für viele Websites der bevorzugte Technologie -Stack ist, umfassen die Benutzerfreundlichkeit, die starke Unterstützung der Community und die weit verbreitete Verwendung. 1) Einfach zu erlernen und zu bedienen, geeignet für Anfänger. 2) eine riesige Entwicklergemeinschaft und eine reichhaltige Ressourcen haben. 3) in WordPress, Drupal und anderen Plattformen häufig verwendet. 4) Integrieren Sie eng in Webserver, um die Entwicklung der Entwicklung zu vereinfachen.

PHP vs. Python: Anwendungsfälle und Anwendungen PHP vs. Python: Anwendungsfälle und Anwendungen Apr 17, 2025 am 12:23 AM

PHP eignet sich für Webentwicklungs- und Content -Management -Systeme, und Python eignet sich für Datenwissenschafts-, maschinelles Lernen- und Automatisierungsskripte. 1.PHP hat eine gute Leistung beim Erstellen von schnellen und skalierbaren Websites und Anwendungen und wird üblicherweise in CMS wie WordPress verwendet. 2. Python hat sich in den Bereichen Datenwissenschaft und maschinelles Lernen mit reichen Bibliotheken wie Numpy und TensorFlow übertrifft.

See all articles