2 * Copyright © 2019 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core.utils
19 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
20 import java.text.SimpleDateFormat
21 import java.time.LocalDateTime
22 import java.time.ZoneId
23 import java.time.format.DateTimeFormatter
24 import java.util.Calendar
27 fun controllerDate(): Date {
28 val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
29 return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())
32 fun currentTimestamp(): String {
33 val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
34 val formatter = DateTimeFormatter.ofPattern(BlueprintConstants.DATE_TIME_PATTERN)
35 return formatter.format(localDateTime)
38 /** Parse string date in CDS string format */
39 fun String.toControllerDate(): Date {
40 val formatter = SimpleDateFormat(BlueprintConstants.DATE_TIME_PATTERN)
41 return formatter.parse(this)
44 /** Return date to CDS string format */
45 fun Date.currentTimestamp(): String {
46 val formatter = SimpleDateFormat(BlueprintConstants.DATE_TIME_PATTERN)
47 return formatter.format(this)
50 /** Return incremented date for [number] of days */
51 fun Date.addDate(number: Int): Date {
52 val calendar = Calendar.getInstance()
54 calendar.add(Calendar.DATE, number)