Skip to content

Instantly share code, notes, and snippets.

@Jatapiaro
Created September 5, 2017 23:49
Show Gist options
  • Save Jatapiaro/d673b6b080f8ace403e2240faa118075 to your computer and use it in GitHub Desktop.
Save Jatapiaro/d673b6b080f8ace403e2240faa118075 to your computer and use it in GitHub Desktop.
Local time demo for java certification
import java.time.*;
import java.time.temporal.Temporal;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class LocalTimeDemo{
public static void main(String[] args) {
System.out.println("Now: "+LocalTime.now()+"\n--------");
System.out.println("Now with clock: "+LocalTime.now(Clock.systemUTC())+"\n--------");
System.out.println("Now with zone id: "+LocalTime.now(ZoneId.of("Asia/Ho_Chi_Minh"))+"\n--------");
System.out.println("of int hour, int minute: "+LocalTime.of(21,59)+"\n--------");
System.out.println("int hour, int minute, int second "+LocalTime.of(21,59,40)+"\n--------");
System.out.println("int hour, int minute, int second, int nanosecond "+LocalTime.of(21,59,40,10)+"\n--------");
long l = 1238971232;
long l2 = 86399;
System.out.println("of nanoOfDay "+LocalTime.ofNanoOfDay(l)+"\n--------");
System.out.println("of secondsOfDay "+LocalTime.ofSecondOfDay(l2)+"\n--------");
System.out.println("parse "+LocalTime.parse("10:55")+"\n--------");
System.out.println("parse with date formatter"+LocalTime.parse("10:55",DateTimeFormatter.ofPattern("HH:mm"))+"\n--------");
LocalTime lo = LocalTime.now();
LocalTime li = LocalTime.now();
System.out.println("offset "+lo.atOffset(ZoneOffset.of("Z"))+"\n--------");
System.out.println("compare "+lo.compareTo(li)+"\n--------");
System.out.println("equals "+lo.equals(new Integer(19))+"\n--------");
System.out.println("get muchos things : "+lo.getHour()+"//"+lo.getMinute()+"//"+lo.getSecond()+"//"+lo.getNano()+"hash"+lo.hashCode()+"\n--------");
System.out.println("is after using plus nano/minut/second/hour: "+lo.plusHours(1).plusSeconds(1).plusNanos(1).plusMinutes(1).isAfter(li)+"\n--------");
System.out.println("is before using with second/nano/hour/minute: "+lo.isBefore(li.withSecond(59).withMinute(40).withHour(23).withNano(10))+"\n--------");
System.out.println("Tosecond of day: "+li.withSecond(59).withMinute(40).withHour(23).withNano(10).toSecondOfDay()+"\n-----");
System.out.println("To nano of day: "+li.withSecond(59).withMinute(40).withHour(23).withNano(10).toNanoOfDay()+"\n-----");
System.out.println("Minus minutes/nano/second/milis: "+li.minusHours(1).minusMinutes(1).minusSeconds(1).minusNanos(1)+"\n-----");
System.out.println("Max localtime: "+LocalTime.MAX+"\n----------");
System.out.println("Midnight localtime: "+LocalTime.MIDNIGHT+"\n----------");
System.out.println("Noon localtime: "+LocalTime.NOON+"\n----------");
System.out.println("Min localtime: "+LocalTime.MIN+"\n----------");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment