2 * Copyright 2000-2017 JetBrains s.r.o.
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 com.intellij.stats.personalization.impl
19 import com.intellij.stats.personalization.Day
20 import java.text.ParsePosition
21 import java.text.SimpleDateFormat
25 * @author Vitaliy.Bibaev
27 class DayImpl(date: Date) : Day {
28 override val dayOfMonth: Int
29 override val month: Int
30 override val year: Int
33 private val DATE_FORMAT = SimpleDateFormat("dd-MM-yyyy")
35 fun fromString(str: String): Day? {
36 val position = ParsePosition(0)
37 val date = DATE_FORMAT.parse(str, position)
38 if (position.index == 0) return null
44 val calendar = Calendar.getInstance()
46 dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
47 month = calendar.get(Calendar.MONTH)
48 year = calendar.get(Calendar.YEAR)
51 override fun compareTo(other: Day): Int {
52 if (year == other.year) {
53 if (month == other.month) {
54 return dayOfMonth.compareTo(other.dayOfMonth)
56 return month.compareTo(other.month)
58 return year.compareTo(other.year)
61 override fun hashCode(): Int {
62 return Objects.hash(year, month, dayOfMonth)
65 override fun equals(other: Any?): Boolean {
66 if (other != null && other is Day) return compareTo(other) == 0
70 override fun toString(): String {
71 return "$dayOfMonth-$month-$year"