Java Date and Time In Hindi

📔 : Java 🔗

Java programming language में date time के लिए कोई built-in class या कोई predefined method नहीं है। date time handle करने के लिए java हमें java.time package provide कराता है जिसमे कई सारी classes होती हैं -

ClassDescription
LocalDateRepresents date (year, month, day (yyyy-MM-dd))
LocalTimeRepresents time (hour, minute, second and nanoseconds (HH-mm-ss-ns))
LocalDateTimeRepresents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
DateTimeFormatterTo formatter for displaying and parsing date-time objects.

Java display current date

java में current date display करने के लिए java.time package की LocalDate class का now() method का use किया जाता है।

File : DateTime.java

CopyFullscreenClose FullscreenRun
// import the LocalDate class forom java.time package.
import java.time.LocalDate;

public class DateTime {
  public static void main(String[] args) {
    LocalDate current_date = LocalDate.now();
    System.out.println("Current date : "+ current_date);
  }
}
Output
Current date : 2022-08-20

Java display current time

इसी तरह से current time को get करने के लिए LocalTime class का now() method use किया जाता है।

File : DateTime.java

CopyFullscreenClose FullscreenRun
// import the LocalTime class forom java.time package.
import java.time.LocalTime;

public class DateTime {
  public static void main(String[] args) {
    LocalTime current_time = LocalTime.now();
    System.out.println("Current time : "+ current_time);
  }
}
Output
Current time : 04:17:35.776296

Java display current date and time

date और time को एक साथ get करने के लिए LocalDateTime class का now() method use किया जाता है।

File : DateTime.java

CopyFullscreenClose FullscreenRun
// import the LocalDateTime class forom java.time package.
import java.time.LocalDateTime;

public class DateTime {
  public static void main(String[] args) {
    LocalDateTime current_date_time = LocalDateTime.now();
    System.out.println("Current date time : "+ current_date_time);
  }
}
Output
Current date time : 2022-08-20T04:20:26.019821

Java Formatting Date and Time

अभी तक जो भी हमें date या time मिल रहा था वो , by default मिल रहा था , लेकिन हो सकता है कि जरूरत पड़ने पर हमें ये format change करना पड़ जाए। date time format करने के लिए DateTimeFormatter class का ofPattern() method का use किया जाता है।

File : DateTime.java

CopyFullscreenClose FullscreenRun
// Import the LocalDateTime and DateTimeFormatter class from java.time package.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormat {
  public static void main(String[] args) {
    LocalDateTime current_date_time = LocalDateTime.now();
    System.out.println("Default : " + current_date_time);
    DateTimeFormatter format1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
    String custome_date_time = current_date_time.format(format1);
    System.out.println("Format 1: " + custome_date_time);
    
    // Other formats.
    DateTimeFormatter format2 = DateTimeFormatter.ofPattern("dd-MM-YY HH:mm");
    custome_date_time = current_date_time.format(format2);
    System.out.println("Format 2: " + custome_date_time);
  }
}
Output
Default : 2022-08-20T04:31:04.311201
Format 1: 20-08-2022 04:31:04
Format 2: 20-08-22 04:31

ofPattern() method , कई अलग अलग date time formats को भी support करता है।

FormatExample
yyyy-MM-dd2022-08-29
dd/MM/yyyy29/08/2022
dd-MMM-yyyy29-Aug-2022
E, MMM dd yyyyThu, Aug 29 2022

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers