I can use the below code to get the output as weekNumber: Int = 1 . But my use case require the output as weekNumber: String= 01 . What should be the best efficient way to do approach this?
import java.time.format.DateTimeFormatter
import java.time.temporal.WeekFields
import java.util.Locale
import java.time.LocalDate
var dateRangeFrom = "20200101"
var date :java.time.LocalDate = LocalDate.parse(dateRangeFrom,DateTimeFormatter.ofPattern("yyyyMMdd"))
var weekFields :WeekFields = WeekFields.of(Locale.getDefault());
var weekNumber :Int = date.get(weekFields.weekOfWeekBasedYear());
Output - weekNumber: Int = 1
You can use DateTimeFormatter with the pattern ww
:
var weekFormat = DateTimeFormatter.ofPattern("ww");
var weekNumber = weekFormat.format(date);
"w" is the week of year, "ww" is week of year with two digits (zero fill). You can see the list of formatting codes in the API documentation. https://docs.oracle.com/javase/10/docs/api/java/time/format/DateTimeFormatter.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.