49528

Combining two Date instance to create date-time using Joda

Question:

I have two java.util.Date instances which is contain date value and time value. now I want to combine these values to create single java.util.Date instance representing the date and time.

here some example to make clear what I'd want :

Date date = 2015-06-01; Date time = 22:30;

combine into :

Date dateTime = 2015-06-01 22:30;

I do some search and I found this question <a href="https://stackoverflow.com/questions/1260112/combining-java-util-dates-to-create-a-date-time" rel="nofollow">Combining java.util.Dates to create a date-time</a> which is similar with my current issue. But the chosen answer on that question is deprecated.

Answer1:

You can do it without JODA, by using Calendar

However, as you asked about JODA, here is the way to do in JODA:

// you want the date part from it Date d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2013-01-02 03:04:05"); // you want to time part from it Date t = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2014-02-03 04:05:06"); LocalDate datePart = new LocalDate(d); LocalTime timePart = new LocalTime(t); LocalDateTime dateTime = datePart.toLocalDateTime(timePart); Date result = dateTime.toDate(); // Or shrink the above 4 lines into one, as follow // Date result = new LocalDate(d).toLocalDateTime(new LocalTime(t)).toDate(); System.out.println("result " + result); // print out result Wed Jan 02 04:05:06 CST 2013

Answer2:

Use a Calendar instead?

In particular, <a href="https://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#set(int,%20int,%20int,%20int,%20int)" rel="nofollow">set(int year, int month, int date, int hourOfDay, int minute)</a> and<br /> if you want a Date, use <a href="https://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getTime()" rel="nofollow">getTime()</a> ?

or convert the Date object to a Calendar object using the setTime(Date ..) function from the Calendar class, extract the values (day, hours, minute) using functions from the Calendar class?

Recommend

  • R - Get current time in milliseconds
  • Java Arabic (Hirji) Calendar
  • Gradle compile dependencies not included in Jar
  • Controls in ItemTemplate can't be called in Code Behind
  • Serialize Date, Instant to ISO 8601 when using JSR310 java.time in spring
  • Parsing Java String to date
  • Create unique ids for a group
  • CSS Sprites don't work in IE[8/7/6]
  • how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsi
  • Difference between two particular dates [duplicate]
  • HTTPS + gzip: Is it a security vulnerability if I only gzip non-sensitive files?
  • How to produce a Code 39 that can be reliably read after faxing
  • PHP Carbon “month()” method generates wrong DateTime
  • Android: Time calculations, how to add a specified amount of minutes
  • Year, Month, and Day parameters describe an un-representable DateTime in Persian calender
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Visual Studio 2010 - 2015 does not use ymm* registers for AVX optimization
  • Java Date object constructor for getting string is deprecated
  • A simple datepicker in VueJS
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • How to parsing NSDate to RFC 822 always use in English?
  • iOS Date formatting
  • Pandas: merge_asof() sum multiple rows / don't duplicate
  • Hibernate Joda DateTime Sorting
  • unable to get jsonEncode in magento2
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • how to avoid repetitive constructor in children
  • Spark fat jar to run multiple versions on YARN
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • ActionScript 2 vs ActionScript 3 performance
  • jQuery tmpl and DataLink beta
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Data Validation Drop Down Box Arrow Disappearing
  • json Serialization in asp
  • WPF Applying a trigger on binding failure
  • Java static initializers and reflection
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?