개발기술/Java

Java : 범용 library 및 Annotation

bsh6226 2024. 5. 5. 22:03
  • java.math package
    • BigInteger, BigDecimal:Represents arbitrary-precision integers with no upper or lower bounds
      • Constructor
        • BigInteger(String val/integer): Creates a BigInteger instance from a string representation of an integer.
        • BigDecimal(String val/double val): Creates a BigDecimal instance from a double value.
      • Operations:
        • add(BigInteger val), subtract(BigInteger val), multiply(BigInteger val), divide(BigInteger val), mod(BigInteger val)
        • pow(int exponent): Raises this instance to the power of the specified exponent.
        • compareTo(BigInteger val): Compares this instance with another BigInteger, returning -1, 0, or 1.
        • instance data conversion : instance.toString() / instance.xxxvalue()
  • java.math class
    • abs(val) : Returns the absolute value of an argument.
    • max,min (two value): Returns the greater, smaller of two values.
    • pow(base val, power val) : Returns the value of the first argument raised to the power of the second argument
    • ceil(val): Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
    • floor(val): Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
    • round(val): Returns the closest long or int, as indicated by the method's return type, to the argumen

 

시간관련 라이브러리 : java.time package

 

 LocalDateTime : 상대 시간 또는 달력 시간 

  • 인간 사회가 문화적·지리적 시간대를 반영하여 만든 시간 개념
  • class0 : LocalDate
    • Format: yyyy-MM-dd (e.g., 2024-07-31).
  • class1 : LocalDateTime : Represents a date-time without a time-zone\
    • Format: yyyy-MM-ddTHH:mm:ss (e.g., 2024-07-31T12:00:00).
    • method 1 : now(): Obtains the current date-time from the system clock in the default time-zone.
    • method2 : of(int year, int month, int dayOfMonth, int hour, int minute): Obtains an instance of LocalDateTime 
    • plusDays(long daysToAdd): Adds a specific number of days to the date.
    • method3 : format(DateTimeFormatter formatter): 매개변수 formatter에서 정의된대로 다듬어서 string 반환
  • class2 : DateTimeFormatter
    • Formatter for printing and parsing date-time objects.
    • method1 : ofPattern(String pattern): Creates a formatter using the specified pattern.

 UTC time : 절대 시간 (Absolute Time) 

  • 정의: 전 우주 어디서든 동일한 기준으로 측정되는 시간,

절대시간 : long type

  • System.currentTimeMillis() : Epoch 기반 밀리초 단위로 long type으로 반환함

절대시간 : instant type

Instant는 단순한 숫자(long)보다 타입 안전성, 가독성, 정확한 연산 지원, 의도 표현력이 강력.

  • Instant.now() :  현재 UTC 시간을 객체로 생성함  (초/나노초 단위)
  • Instant.ofEpochSecond(long) : 에포크 초 기준으로 생성
  • Instant.ofEpochMilli(long) : 에포크 밀리초 기준으로 생성

타입간의 시간변환

  • long type 에서 instant타입 변환 : Instant instant = Instant.ofEpochMilli(long);
  • instant type에서 UTC 시간대의 LocalDateTime으로 변환 : LocalDateTime.ofInstant(Instant, ZoneId.of("UTC")) 

 

  • java.util.Random
    • the package generate pseudorandom numbers. The seed is derived from current time in millisecond.  You can either pass a seed explicitly to the constructor of a Random object 
    • method
      • nextBoolean(): Returns a random boolean value.
      • nextInt(): Returns a random integer.
      • nextInt(int bound): Returns a random integer between 0 (inclusive) and the specified bound (exclusive).
      • nextLong(): Returns a random long.
      • nextDouble(): Returns a random double between 0.0 (inclusive) and 1.0 (exclusive).
      • nextFloat(): Returns a random float between 0.0f (inclusive) and 1.0f (exclusive).
  •  java.util.UUID
    • UUID :  Universal Unique Identifier, which is a standardized method of generating identifiers that are unique across different systems and networks 
      • A UUID is a 128-bit number, typically expressed as a string of 32 hexadecimal characters and broken down into five groups, separated by hyphens. This format is represented as 8-4-4-4-12, making a typical UUID look like this: 123e4567-e89b-12d3-a456-426614174000.
    • randomUUID(): Generates a random UUID (Version 4).
      • UUID uuid = UUID.randomUUID();
    • toString(): Returns a UUID as a string.

 

Java annotation

to provide metadata for methods where it is applied

 

 

@Target(ElementType.METHOD):

  • Specifies that this annotation can only be applied to methods.

@Retention(RetentionPolicy.RUNTIME):

  • Indicates that this annotation will be available at runtime. : This is necessary if you want to use reflection or an aspect-oriented programming (AOP) library (e.g., Spring AOP) to process this annotation.
  •  
  •