[JAVA] Date processing

Introduction

I often use the processing of date data. About the format method.

Java This is my favorite format in Java.

java


private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(new Date());
try {
	Date now = format.parse(date);
} catch (ParseException e) {
	e.printStackTrace();
}

JavaScript I wanted to pass it in the format of yyyy-MM-dd, so I got it by the following method.

javascript


//day Set the date after the day
function addDay(date, day) {
	date.setDate(date.getDate()+day);
	return date;
}

//Get formatted characters
function getFormatDateString(date) { 
	date.setDate(date.getDate());
	var year  = date.getFullYear();
	var month = date.getMonth() + 1;
	month = ("00" + month).slice(-2);
	var day   = date.getDate();
	day = ("00" + day).slice(-2);
	return String(year) + "-" + String(month) + "-" + String(day);
}

function getFormatTimeString(date) {
	return date.toTimeString().substring(0,8);
}

JavaScript Date function output example

I executed and confirmed the output of a function that seems to be usable. date.toLocaleTimeString (); is the same as getFormatTimeString above. ..

console


date = new Date();
date.toDateString();
"Mon Jun 11 2018"
date.toGMTString();
"Mon, 11 Jun 2018 11:20:26 GMT"
date.toISOString();
"2018-06-11T11:20:26.537Z"
date.toJSON();
"2018-06-11T11:20:26.537Z"
date.toLocaleDateString();
"2018/6/11"
date.toLocaleString();
"2018/6/11 20:20:26"
date.toLocaleTimeString();
"20:20:26"
date.toSource();
"(new Date(1528716026537))"
date.toString();
"Mon Jun 11 2018 20:20:26 GMT+0900"
date.toTimeString();
"20:20:26 GMT+0900"
date.toUTCString();
"Mon, 11 Jun 2018 11:20:26 GMT"

reference

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date Date processing such as date format --Qiita

Recommended Posts

Date processing
Date processing in Java (LocalDate: Initialization)
Loop processing
Iterative processing
[Ruby] Iterative processing
Java thread processing
Java string processing
Date () and Calendar ()
Ruby Iterative Processing
[Java] Multi-thread processing
Stream intermediate processing
Christmas with Processing
[Java] Stream processing
Create versatile processing
java iterative processing