diff --git a/app/build.gradle b/app/build.gradle index c60553e..26bfbb1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,5 +31,7 @@ dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' + implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1' + implementation project(path: ':core') } diff --git a/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt b/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt index 73b74ba..e550779 100644 --- a/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt +++ b/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt @@ -3,6 +3,7 @@ package com.oxeanbits.forecastchart.ui import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity +import com.jakewharton.threetenabp.AndroidThreeTen import com.oxeanbits.forecastchart.core.ui.component.forecastChartComponent import com.oxeanbits.forecastchart.util.SetupChartExample import trikita.anvil.BaseDSL.MATCH @@ -13,6 +14,7 @@ class MainExample : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + AndroidThreeTen.init(this) setContentView(getView()) } diff --git a/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt b/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt index 73bfd64..0bd8157 100644 --- a/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt +++ b/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt @@ -4,22 +4,19 @@ import android.graphics.Color import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.utils.ColorTemplate import com.oxeanbits.forecastchart.core.model.Line -import java.text.SimpleDateFormat +import com.oxeanbits.forecastchart.core.util.DateFormatter object SetupChartExample{ - const val THOUS = 1000f - val dateFormat = SimpleDateFormat("yyyy-MM-dd") - - val timestamp1 = (dateFormat.parse("2019-01-01").time)/THOUS + val timestamp1 = DateFormatter.stringToTimestamp("2019-01-01") val progress1 = 25f - val timestamp2 = (dateFormat.parse("2019-01-02").time)/THOUS + val timestamp2 = DateFormatter.stringToTimestamp("2019-01-02") val progress2 = 25f + progress1 - val timestamp3 = (dateFormat.parse("2019-01-03").time)/THOUS + val timestamp3 = DateFormatter.stringToTimestamp("2019-01-03") val progress3 = 25f + progress2 - val timestamp4 = (dateFormat.parse("2019-01-04").time)/THOUS + val timestamp4 = DateFormatter.stringToTimestamp("2019-01-04") val progress4 = 0f + progress3 - val timestamp5 = (dateFormat.parse("2019-01-05").time)/THOUS + val timestamp5 = DateFormatter.stringToTimestamp("2019-01-05") val progress5 = 25f + progress4 fun getExpectedObj(): Line{ diff --git a/core/build.gradle b/core/build.gradle index f71e237..fa287f0 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -33,4 +33,6 @@ dependencies { implementation 'co.trikita:anvil-design:0.5.2' implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' + + implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1' } \ No newline at end of file diff --git a/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt b/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt index 8a2d6d9..bb78cdd 100644 --- a/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt +++ b/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt @@ -1,12 +1,22 @@ package com.oxeanbits.forecastchart.core.util -import java.text.SimpleDateFormat -import java.util.Date +import org.threeten.bp.Instant +import org.threeten.bp.LocalDate +import org.threeten.bp.LocalDateTime +import org.threeten.bp.ZoneOffset +import org.threeten.bp.format.DateTimeFormatter + object DateFormatter{ + + fun stringToTimestamp(date: String): Float{ + return LocalDate.parse(date).atStartOfDay() + .toInstant(ZoneOffset.UTC).epochSecond.toFloat() + } + fun timestampToDate(timestamp: Long): String{ - val mDate = Date() - mDate.time = (timestamp.times(1000)) - return SimpleDateFormat("yyyy-MM-dd").format(mDate) + val mDate = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneOffset.UTC) + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") + return mDate.format(formatter) } } \ No newline at end of file