java-time

Java: check if a given date is within current month

那年仲夏 提交于 2020-03-17 16:24:25
问题 I need to check if a given date falls in the current month, and I wrote the following code, but the IDE reminded me that the getMonth() and getYear() methods are obsolete. I was wondering how to do the same thing in newer Java 7 or Java 8. private boolean inCurrentMonth(Date givenDate) { Date today = new Date(); return givenDate.getMonth() == today.getMonth() && givenDate.getYear() == today.getYear(); } 回答1: Time Zone The other answers ignore the crucial issue of time zone. A new day dawns

Java: check if a given date is within current month

落爺英雄遲暮 提交于 2020-03-17 16:24:07
问题 I need to check if a given date falls in the current month, and I wrote the following code, but the IDE reminded me that the getMonth() and getYear() methods are obsolete. I was wondering how to do the same thing in newer Java 7 or Java 8. private boolean inCurrentMonth(Date givenDate) { Date today = new Date(); return givenDate.getMonth() == today.getMonth() && givenDate.getYear() == today.getYear(); } 回答1: Time Zone The other answers ignore the crucial issue of time zone. A new day dawns

How to skip weekends while adding days to LocalDate in Java 8?

て烟熏妆下的殇ゞ 提交于 2020-03-12 07:34:41
问题 Other answers here refer to Joda API. I want to do it using java.time . Suppose today's date is 26th Nov 2015-Thursday, when I add 2 business days to it, I want the result as Monday 30th Nov 2015. I am working on my own implementation but it would be great if something already exists! EDIT: Is there a way to do it apart from looping over? I was trying to derive a function like: Y = f(X1,X2) where Y is actual number of days to add, X1 is number of business days to add, X2 is day of the week (1

Bug in jdk8 date-conversion?

被刻印的时光 ゝ 提交于 2020-02-19 12:35:27
问题 I was writing some testcode for java-8 conversion between java.util.Date and java.time.LocalDateTime , and discovered an anomaly seems to occur in the hour after the transition from normaltime-to-summertime, when the year is 2038 or higher. I just wanted to know if this is a bug in jdk8, or if I am doing something wrong? Note: I am on Windows-7, 64-bit jdk, so should not be affected by the 2038-unix bug, which would have a much worse effect. Here my demo-code: package conversiontest; import

JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone

旧城冷巷雨未停 提交于 2020-02-11 06:21:08
问题 Could you please shed some light on how to obtain correct epoch time in milliseconds for a default system timezone and given timezone. Given 1. TimeZone: GMT+3 2. The following code snippet: import java.time.*; public class Main { public static void main(String[] args) { System.out.println(LocalDateTime .now() .atZone(ZoneOffset.UTC) .toInstant() .toEpochMilli() ); System.out.println(LocalDateTime .now() .atZone(ZoneOffset.of("+3")) .toInstant() .toEpochMilli() ); System.out.println(System

How to get Time Slot interval of 1Hour based two times android

家住魔仙堡 提交于 2020-02-06 09:49:10
问题 How to get Time Slot interval of 1Hour based two times androidI want to store time slot in the arraylist. i have start time and end time. based on start time it should create time slot. For example if start time is 09:00AM and end time is 21:00PM then it should add into arraylist like below 09:00AM 10:00AM 11:00AM 12:00PM 13:00PM 14:00PM ..... so on 21:00PM 回答1: You can do this way : String firstDate = "26/02/2019"; String firstTime = "00:00 AM"; String secondDate = "26/02/2019"; String

Duration with leap seconds

╄→尐↘猪︶ㄣ 提交于 2020-02-03 04:40:30
问题 I need to schedule a task in my code at a fixed datetime. For that I'm using a ScheduledExecutorService with the method schedule(Runnable command, long delay, TimeUnit unit); How can I compute this delay according to leap seconds ? For the moment I use Duration.between() but it doesn't seem aware of leap seconds: ZonedDateTime date1 = ZonedDateTime.of(2015, 06, 30, 23, 59, 59, 000000, ZoneOffset.UTC); ZonedDateTime date2 = ZonedDateTime.of(2015, 07, 01, 00, 00, 00, 000000, ZoneOffset.UTC);

How to create a long time in Milliseconds from String in Java 8 with LocalDateTime?

一笑奈何 提交于 2020-02-02 04:59:45
问题 I have an input date in format yyyy-MM-dd_HH:mm:ss.SSS and convert it to long this way: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS"); try { Date date = simpleDateFormat.parse(lapTime); time = date.getTime(); } catch (ParseException e) { e.printStackTrace(); } And, after some manipulation, get mm:ss.SSS back from long: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss.SSS"); return simpleDateFormat.format(new Date(time)); How to change my

How to get time in slot type in android

淺唱寂寞╮ 提交于 2020-01-24 19:34:19
问题 I am trying to get time in slot type. for example bike shop is open 9am to 9pm so we can get the bike in that time. if anyone books a bike in evening 5 pm to 7 pm slot then it should show 9am to 5pm (available), 5pm to 7pm (not available) and 7pm to 9pm (available) slots. so that another user who wants to book same bike they can understand the bike is not available for this slot and he can book on only available slots. me how to achieve this? 回答1: A model class to hold data class TimeSlot {

Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()

隐身守侯 提交于 2020-01-21 14:07:34
问题 In Java, we can have many different ways to get the current timestamp, but which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis() 回答1: Both are fine. And neither is recommended except for a minority of purposes. What do you need milliseconds since the epoch for? In Java, we can have many different ways to get the current timestamp, For current timestamp just use Instant.now() . No need to convert to milliseconds. Many methods from the first years of Java, also