java.util.date

Are java.util.Date and java.util.Calendar deprecated?

瘦欲@ 提交于 2019-11-29 20:53:45
问题 It seems that the new java.time API offers everything from java.util.Date and much more. Is there any reason to use java.util.Date when the newer java.time API is there since Java 8? Should java.util.Date and java.util.Calendar be avoided completely? 回答1: Short answer: The new API java.time is way better than the old world with java.util.Date and java.util.Calendar . So yes, the new API should be preferred in new code. For a quick overview: Once I had written a comparison of features in table

How would you represent date of birth in your java model?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 14:18:55
问题 And wait, don't rush to answer "java.util.Date", consider the following scenario. Person object having 2 fields: "birthday" and "nextMeeting" both java.util.Date. Now birthday stored in database as date type column (no time) for eg. 01-10-1979, and nextMeeting as datetime type for ex. 01-10-2010 20:00:00. You pull it from db, "birthday" will be auto set to midnight by JDBC. Now you need to send this object to other JVM using lets say RMI or whatever technology. On the other end JVM has

How to remove milliseconds from Date Object format in Java

旧街凉风 提交于 2019-11-29 03:48:55
Since the java.util.Date object stores Date as 2014-01-24 17:33:47.214 , but I want the Date format as 2014-01-24 17:33:47 . I want to remove the milliseconds part. I checked a question related to my question... How to remove sub seconds part of Date object I've tried the given answer long time = date.getTime(); date.setTime((time / 1000) * 1000); but I've got my result Date format as 2014-01-24 17:33:47.0 . How can I remove that 0 from my Date format??? Basic answer is, you can't. The value returned by Date#toString is a representation of the Date object and it carries no concept of format

Get date representation in seconds?

这一生的挚爱 提交于 2019-11-29 03:41:20
I am using an API which requires a date parameter as a number of seconds, an int . My problem is that I currently store this time in java.util.date and I was wondering if there is some way to convert the java.util.date variable to seconds so that I can fit it into the int parameter which the API requires? import java.util.Date; ... long secs = (new Date().getTime())/1000; ... Please see - http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() java.util.Date.getTime() it returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. java

How to convert Joda-Time DateTime to java.util.Date and vice versa?

两盒软妹~` 提交于 2019-11-28 04:03:39
Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa? To convert Java Date to Joda DateTime :- Date date = new Date(); DateTime dateTime = new DateTime(date); And vice versa:- Date dateNew = dateTime.toDate(); With TimeZone , if required:- DateTime dateTimeNew = new DateTime(date.getTime(), timeZone); Date dateTimeZone = dateTime.toDateTimeAtStartOfDay(timeZone).toDate(); You haven't specified which type within Joda Time you're interested in, but: Instant instant = ...; Date date = instant.toDate(); instant = new Instant(date); // Or...

How to convert ZonedDateTime to Date?

只愿长相守 提交于 2019-11-28 03:45:22
I am trying to set a server agnostic date time in my database and I believe the best practice to do so is to set a UTC DateTime. My db server is Cassandra and the db driver for Java understands only the Date type. So assuming that in my code I am using the new Java 8 ZonedDateTime to get the UTC now ( ZonedDateTime.now(ZoneOffset.UTC) ), how can I convert this ZonedDateTime instance to the "legacy" Date class? Slim Soltani Dridi You can convert ZonedDateTime to an instant, which you can use directly with Date. Date.from(java.time.ZonedDateTime.now().toInstant()); tl;dr java.util.Date.from( //

does System.currentTimeMillis() return UTC time?

你。 提交于 2019-11-28 03:31:53
I want to get the current UTC time in millis. I searched google and got some answers that System.currentTimeMillis() does returns UTC time. but it does not. If I do following: long t1 = System.currentTimeMillis(); long t2 = new Date().getTime(); long t3 = Calendar.getInstance().getTimeInMillis(); all three times are almost same ( difference is in milli seconds due to calls ). t1 = 1372060916 t2 = 1372060917 t3 = 1372060918 and this time is not the UTC time instead this is my timezone time. How can i get the current UTC time in android? All three of the lines you've shown will give the number

java.util.Date is generating a wrong date?

本小妞迷上赌 提交于 2019-11-28 02:28:49
Here's my code: java.util.Date TODAY = new java.util.Date(); SimpleDateFormat SDF = new SimpleDateFormat( "YYYY-MM-DD" ); System.out.println ( SDF.format( TODAY ) );' And the result is: 2015-02-33 But today's date is 2015-02-02! What may be the reason behind this wrong output? What may be the reason behind this Wrong Output ? Your assumptions about the date format string are wrong, the output is correct . y Year Y Week year D Day in year d Day in month M Month in year m Minute in hour http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html Y Week year will usually give

How can I create a Date object with a specific format

ぐ巨炮叔叔 提交于 2019-11-28 02:10:31
String testDateString = "02/04/2014"; DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date d1 = df.parse(testDateString); String date = df.format(d1); Output String: 02/04/2014 Now I need the Date d1 formatted in the same way ( "02/04/2014" ). If you want a date object that will always print your desired format, you have to create an own subclass of class Date and override toString there. import java.text.SimpleDateFormat; import java.util.Date; public class MyDate extends Date { private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); /* * additional constructors */

How to add minutes to my Date

主宰稳场 提交于 2019-11-27 06:39:01
I have this date object: SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm"); Date d1 = df.parse(interviewList.get(37).getTime()); value of d1 is Fri Jan 07 17:40:00 PKT 2011 Now I am trying to add 10 minutes to the date above. Calendar cal = Calendar.getInstance(); cal.setTime(d1); cal.add(Calendar.MINUTE, 10); String newTime = df.format(cal.getTime()); Value of newTime changes to 2011-50-07 17:50 but it should be 07-01-2011 17:50 . It adds minutes correctly but it also changes month, don't know why! Aravind R. Yarram The issue for you is that you are using mm . You should use MM .