java.util.date

Do we have a TimeSpan sort of class in Java

烂漫一生 提交于 2019-11-26 11:19:51
问题 I was just wondering if there is a need of TimeSpan in java.util so that I can define how much hours,minutes and seconds are there in between these two times. From this TimeSpan we can have a time interval between two times. like TimeSpan getTimeSpan( Date before, Date after ){...} or long timeSpan = System.currentTimeMillis(); // ... long job timeSpan = System.currentTimeMillis() - timeSpan; TimeSpan ts = new TimeSpan(timeSpan); and with this TimeSpan we can use it in SimpleDateFormat .

How to subtract X day from a Date object in Java?

我怕爱的太早我们不能终老 提交于 2019-11-26 08:06:43
问题 I want to do something like: Date date = new Date(); // current date date = date - 300; // substract 300 days from current date and I want to use this \"date\" How to do it? 回答1: Java 8 and later With Java 8's date time API change, Use LocalDate LocalDate date = LocalDate.now().minusDays(300); Similarly you can have LocalDate date = someLocalDateInstance.minusDays(300); Refer to https://stackoverflow.com/a/23885950/260990 for translation between java.util.Date <--> java.time.LocalDateTime

How to set time zone of a java.util.Date?

牧云@^-^@ 提交于 2019-11-26 01:20:05
问题 I have parsed a java.util.Date from a String but it is setting the local time zone as the time zone of the date object. The time zone is not specified in the String from which Date is parsed. I want to set a specific time zone of the date object. How can I do that? 回答1: Use DateFormat. For example, SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); isoFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = isoFormat.parse("2010-05-23T09:01:02"); 回答2: Be aware

Convert java.util.Date to String

时光总嘲笑我的痴心妄想 提交于 2019-11-25 22:44:52
问题 I want to convert a java.util.Date object to a String in Java. The format is 2010-05-30 22:15:52 回答1: Convert a Date to a String using DateFormat#format method: String pattern = "MM/dd/yyyy HH:mm:ss"; // Create an instance of SimpleDateFormat used for formatting // the string representation of date according to the chosen pattern DateFormat df = new SimpleDateFormat(pattern); // Get the today date using Calendar object. Date today = Calendar.getInstance().getTime(); // Using DateFormat format

Calculating the difference between two Java date instances

此生再无相见时 提交于 2019-11-25 22:13:23
问题 I\'m using Java\'s java.util.Date class in Scala and want to compare a Date object and the current time. I know I can calculate the delta by using getTime(): (new java.util.Date()).getTime() - oldDate.getTime() However, this just leaves me with a long representing milliseconds. Is there any simpler, nicer way to get a time delta? 回答1: The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library. Joda Time has a concept of time Interval: Interval interval = new