0

what I am trying to do is get a start date and last date of the last month and also the first date of the year using the native android libraries. I have done it using LocalDate, but since it has API restriction I have to implement it so it works on all devices.

    //Get last month first data and return it as a string
    private fun getStartDateLastMonth(): String {
        val startDate= LocalDate.now()
            .minusMonths(1)
            .minusDays(LocalDate.now().dayOfMonth.toLong() - 1)
        return startDate.toString()
    }
    
    //Get last month last date and retur it as a string
    private fun getEndDateLastMonth(): String {
        val endDate = LocalDate.now()
            .minusMonths(1)
            .minusDays(LocalDate.now().dayOfMonth.toLong())
            .plusMonths(1)
        return endDate.toString()
    }
    
    //Get first day of the year
    private fun getStartDateThisYear(): String {
        val startDate = LocalDate.now()
            .with(TemporalAdjusters.firstDayOfYear())
        return startDate.toString()
    }
    
    //Get today 
    private fun getEndDateThisYear(): String {
        val endDate = LocalDate.now()
        return endDate.toString()
    }

The output has to be string but has to have this format "yyyy-MM-dd"

Any help or sugestion would be great. thanks

jvargas
  • 713
  • 1
  • 5
  • 13
  • You want december 1, december 31 and januari 1. I wonder why you think you need a library to generate strings 2034-12-01, 2034-12-31 and 2034-01-02 given year 2034. – blackapps Mar 10 '23 at 18:26
  • Does this answer your question? [DateTimeFormatter is not working in Android versions lower than 8](https://stackoverflow.com/questions/57203186/datetimeformatter-is-not-working-in-android-versions-lower-than-8). TL;DR: Use desugaring and continue using `LocalDate` also for old devices with low API levels. – Ole V.V. Mar 11 '23 at 11:30
  • Not at al answering what you asked, I think that [this is a better way to get the dates you want](https://rextester.com/MRXB42817). The point is only to read the clock once to ensure consistent results if the code is run over midnight between two months or someone tampers with the time zone setting of your JVM. You are aware, of course, that running the code in January means that last month is outside this year. (I am still recommending desugaring for using this code on old Android devices if you are allowed to.) – Ole V.V. Mar 12 '23 at 10:06
  • @blackapps Using a good date library is certainly recommended even though I see your point. If faced with only the cumbersome, heavy-weight and long outdated `GregorianCalendar` class, I think I would still swallow the bitter pill, but the choice in this case isn’t clear, so I agree with you this far. – Ole V.V. Mar 12 '23 at 10:35
  • Also, @blackapps, finding the last day of last month is non-trivial as string operations alone considering leap years. – Ole V.V. Mar 12 '23 at 12:36
  • For januari and december? – blackapps Mar 12 '23 at 15:23
  • @blackapps *start date and last date of the last month* The way I read the OP’s text and code they are after the first and last day of the previous month in relation to when the code is run. So here in March last month would be February this year and the last day 2023-02-28. Next year at the same time last month will be February 2024, and the last day will be the 29th. – Ole V.V. Mar 12 '23 at 16:53
  • thank you all for the reply. you are correct on the january case that would get me back december 1 and 31. I will check the answers you sugested – jvargas Mar 13 '23 at 12:32

0 Answers0