Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / utils / DateUtils.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.utils
18
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
25 import java.util.Date
26
27 fun controllerDate(): Date {
28     val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
29     return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())
30 }
31
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)
36 }
37
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)
42 }
43
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)
48 }
49
50 /** Return incremented date for [number] of days */
51 fun Date.addDate(number: Int): Date {
52     val calendar = Calendar.getInstance()
53     calendar.time = this
54     calendar.add(Calendar.DATE, number)
55     return calendar.time
56 }