/ W3SCHOOLS

W3schools - Scanner / Date

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Scanner

The Scanner class is used to get user input and it is found in the “java.util” package

To use the “Scanner” class

// ----1. Create an object of the class----
Scanner myObj = new Scanner(System.in);
System.out.println(Enter username);

// ----2. Use any of the available methods----
String userName = myObj.nextLine();
System.out.println(Username is :  + userName);

// methods found in the “Scanner” class documentation
// (nextDouble(), nextInt(),... each data type

Date

import java.time package to work with the date and time API

The package includes many date and time classes

Class Description
LocalDate Represents a date (yyyy-MM-dd)
LocalTime Represents a time(HH-mm-ss-ns)
LocalDateTime Represents both a date and a time
DateTimeFormatter Formatter for displaying and parsing date time objects

Display Current

import java.time.LocalDateTime;		// import LocalDateTime class
import java.time.format.DateTimeFormatter // import the DateTimeFormatter class
public class Main{
	public static void main(String[] args){
		LocalDateTime myObj = LocalDateTime.now();	// create a date object
		System.out.print(myObj);	// output 2021-12-19T09:00:00.839828
		DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern(dd-MM-yy HH:mm:ss);
		String formattedDate = myObj.format(myFormatObj);
		System.out.print(formattedDate); // output 19-12-2021 09:00:00
	}
}

ofPattern() method accepts all sorts of values

  • “yyyy-MM-dd”, “dd/MM/yyy”, “dd-MMM-yyy”, “E,MMM dd yyy”