What's new

Closed Tidbit code - date and time - old and new api

Status
Not open for further replies.

codyscott

Eternal Poster
Joined
Sep 13, 2017
Posts
388
Reaction
454
Points
279
Demo kung kelan at kung paano ang paggamit ng Date and Time

Para sa mga older version ng Java programs or apps, use this (OLD API)
Code:
    //as is
     Date now = new Date();
     System.out.println(now);
      
     //explicit
     GregorianCalendar gc = new GregorianCalendar(2009,1,28);
     Date dl = gc.getTime();
     System.out.println(dl);
      
     //take note: older API is 0 based, meaning 0 is January, 1 is February
     gc.add(GregorianCalendar.DATE, 1);
     Date d2 = gc.getTime();
     System.out.println(d2);
    
     //formatting
     DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
     System.out.println(df.format(d2));


Ito ang bago for newer Java programs or apps (LATEST API)
Code:
     //as is
     LocalDateTime ldt = LocalDateTime.now();
     System.out.println(ldt);

     //explicit
     //take note, new API is 1 based, meaning 1 is January, 2 is February
     LocalDate ld = LocalDate.of(2009,1,28);
     System.out.println(ld);

      //formatting
     DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM d, uu");
     System.out.println(dtf.format(ld));

TAKE NOTE: Basahin ang documentation para alamin kung ano pa ang mga methods na puwedeng gamitin (like adding, subtracting, other formatting layout for Date and Time)

- Isang puwedeng paggamitan ay sukatin kung gaano kabilis ang execution ng inyong program. Kung ilang milliseconds (halimbawa lang).
 
Status
Not open for further replies.

Similar threads

Back
Top