2016년 4월 28일 목요일

Java 8의 Date/Time API

지금까지의 Date/Time 관련 API는 개발자에게 외면을 받아왔지만, Java 8에서는 "JSR 310: Data and Time API" Spec을 구현하여 획기적으로 변화했다고 한다. 새로운 Date/Time 클래스는 java.time.* 으로 패키징 되어 있다.


System의 Locale에 따른 날짜 및 시간 형식으로 표현된다. 불변성을 가지고 Thread Safe 하다.


java.time.LocalDate
package org.smarttechie;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
* */
public class LocalDateDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
// Create date
LocalDate localDate = LocalDate.now();
System.out.println("The local date is :: " + localDate);

// Find the length of the month. That is, how many days are there for
// this month.
System.out.println("The number of days available for this month:: "
+ localDate.lengthOfMonth());

// Know the month name
System.out.println("What is the month name? :: "
+ localDate.getMonth().name());

// add 2 days to the today's date.
System.out.println(localDate.plus(2, ChronoUnit.DAYS));

// substract 2 days from today
System.out.println(localDate.minus(2, ChronoUnit.DAYS));

// Convert the string to date
System.out.println(LocalDate.parse("2017-04-07"));
}
}


----------------------------------
The local date is :: 2016-04-28
The number of days available for this month:: 30
What is the month name? :: APRIL
2016-04-30
2016-04-26
2017-04-07


java.time.LocalTime
package org.smarttechie;

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
* */
public class LocalTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
// Get local time
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
// Get the hour of the day
System.out.println("The hour of the day:: " + localTime.getHour());

// add 2 hours to the time.
System.out.println(localTime.plus(2, ChronoUnit.HOURS));
// add 6 minutes to the time.
System.out.println(localTime.plusMinutes(6));
// substract 2 hours from current time
System.out.println(localTime.minus(2, ChronoUnit.HOURS));
}
}

--------------------------------------
13:33:53.602
The hour of the day:: 13
15:33:53.602
13:39:53.602
11:33:53.602

java.time.LocalDateTime
package org.smarttechie;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
*
*/
public class LocalDateTimeDemonstration {

/**
* @param args
*/
public static void main(String[] args) {

// Get LocalDateTime object
LocalDateTime localDateTime = LocalDateTime.now();

System.out.println(localDateTime);

// Find the length of month. That is, how many days are there for this
// month.
System.out.println("The number of days available for this month:: "
+ localDateTime.getMonth().length(true));

// Know the month name
System.out.println("What is the month name? :: "
+ localDateTime.getMonth().name());

// add 2 days to today's date.
System.out.println(localDateTime.plus(2, ChronoUnit.DAYS));

// substract 2 days from today
System.out.println(localDateTime.minus(2, ChronoUnit.DAYS));

}
}
------------------------------------
2016-04-28T13:34:58.061
The number of days available for this month:: 30
What is the month name? :: APRIL
2016-04-30T13:34:58.061
2016-04-26T13:34:58.061


java.time.Period
기간을 Day보다 적은 단위로 표현이 가능하다. 시, 분, 초, 밀리, 나노
package org.smarttechie;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
*
*/
public class PeriodDemonstration {

/**
* @param args
*/
public static void main(String[] args) {

LocalDate localDate = LocalDate.now();

Period period = Period.between(localDate,
localDate.plus(2, ChronoUnit.DAYS));
System.out.println(period.getDays());
}
}


java.time.Duration
Day보다 큰 단위로 표현이 가능. 월, 년
package org.smarttechie;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
* This class demonstrates JAVA 8 data and time API
*
* @author Siva Prasad Rao Janapati
*
*/
public class DurationDemonstration {

/**
* @param args
*/
public static void main(String[] args) {

LocalDateTime localDateTime = LocalDateTime.now();

// Get the duration between two dates
Duration duration = Duration.between(localDateTime,
localDateTime.plus(2, ChronoUnit.DAYS));
System.out.println(duration.toDays());
}
}

java.time.Year
윤년 여부도 알수 있는 Year 클래스
package org.smarttechie;
import java.time.Year;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
*
*/
public class YearDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
  //Get year
  Year year = Year.now();
  System.out.println("Year ::" + year);
  //know the year is leap year or not
  System.out.println("Is year[" +year+"] leap year?"+ year.isLeap());
 }
}



블록체인 개요 및 오픈소스 동향

블록체인(block chain) 블록체인은 공공 거래장부이며 가상 화폐로 거래할때 발생할때 발생할 수 있는 해킹을 막는 기술. 분산 데이터베이스의 한 형태로, 지속적으로 성장하는 데이터 기록 리스트로서 분산 노드의 운영자에 의한 임의 조작이 불가...