datetime - How to set Time property in Java using Joda-Time -
i want set hour, minute , seconds in joda-time. when set it's not changing property.
here code:
import org.joda.time.datetime; public class jodaakbar { public static void main(string args[]) { datetime dt = new datetime(); system.out.println("before:"+dt); dt.hourofday().setcopy(5); dt.minuteofday().setcopy(20); dt.secondofday().setcopy(0); system.out.println("after:"+dt); } }
here output.
before:2015-04-01t11:01:38.277+11:00 after:2015-04-01t11:01:38.277+11:00
i getting same output. what's happening wrong here?
edit:
basically, want similar shown in below code. below code doesn't work 24 hour format, switched joda-time.
calendar cal = calendar.getinstance(); cal.set(calendar.hour, 13); cal.set(calendar.minute, 25); cal.set(calendar.second, 0);
joda-time objects immutable. word "copy" in setcopy
telling doesn't set these fields directly, instead creates copy of datetime
field modified.
a quick fix is:
dt = dt.hourofday().setcopy(5); dt = dt.minuteofhour().setcopy(20); dt = dt.secondofminute().setcopy(0);
a more fluent approach chain several with
methods together:
datetime dt = new datetime() .withhourofday(5) .withminuteofhour(20) .withsecondofminute(0);
or single withtime
call:
datetime dt = new datetime().withtime(5, 20, 0, 0);
by way, java 8 introduces new java.time
package inspired joda-time. joda-time web site recommends, "from java se 8 onwards, users asked migrate java.time
(jsr-310)."
Comments
Post a Comment