How to update the date value alone in a DateTime variable - java

I have a variable of datatype DateTime (org.joda.time.DateTime). I need to update the date value of the variable without modifying the time?
Is it possible to add days to the DateTime variable without modifying time?
[Like how we do for Date (org.apache.commons.lang.time.DateUtils) variable using method DateUtils.addDays()]
Thanks,
Sasank

Have you considered using
http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#withDate%28int,%20int,%20int%29
From JavaDocs
withDate
public DateTime withDate(int year, int monthOfYear, int dayOfMonth)
Returns a copy of this datetime with the specified date, retaining the time fields.
If the date is already the date passed in, then this is returned.

DateTime d = new DateTime();
DateTime fiveDaysLater = d.plusDays(5);
This adds 5 days to your time. I hope this is what you are looking for.
The same works for plusMonths and more see: http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html

Related

How to convert Java's date strings/objects into JFreeChart's Date format?

I'm creating a candlestick chart using the JFreeChart library. I need to pass time values into OHLCSeries, but it only accepts its own time values
I'm going to pass this class https://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/Hour.html
And this constructor: Hour(int hour, int day, int month, int year)
The data which I get from the API originally looks like this: 2021-03-02T16:00:00.000Z
My question: how to convert this date string/Java time formats into JFreeChart's Date format? I can only think about some complicated converting into Java types + returning each value separately, or using regexes
You can parse the date-time string into Instant and then get java.util.Date object from this instant. Using this java.util.Date object, you can create an instance of Hour.
String strDateTime = "2021-03-02T16:00:00.000Z";
Instant instant = Instant.parse(strDateTime);
Hour hour = new Hour(Date.from(instant)));

Compare time using JodaTime in Android

I need to compare whether a DateTime object is greater/less than 9AM PST using JodaTime library in Android.
I am getting the DateTime object in PST like this:
DateTime dt = new DateTime(DateTimeZone.forID( "America/Los_Angeles" ));
How can I compare whether the time recorded for this object is greater/less than 9AM PST?
Thanks.
You can use one of the next ways to do what you want.
1) Create a DateTime with the TimeZone and the hour that you want.
DateTime dt = new DateTime()
.withHourOfDay(9)
.withZone(DateTimeZone.forID("America/Los_Angeles"));
And then compare dt with the DateTime that you receive, create, etc.
dt.isAfter(date) or dt.isBefore(date)
2) You can set the TimeZone that you want to a copy of the DateTime that you receive.
I say copy because DateTime object is immutable. You can not modify the DateTime which you receive but create one from it, yes. You can read more about that here: Why are Joda objects immutable?
datePST = date.withZone(DateTimeZone.forID("America/Los_Angeles"));
int hour = datePST.getHourOfDay()
Now you just need to check if hour is < or > than 9.

Java - Creating a Calendar without a TimeZone

I would like to start by saying that I've read several threads similar to this one, but none of them really solved my problem.
I would also like to state that I've tried to use SimpleDateFormat and joda.DateTime without any success.
The problem is the following:
I have a Calendar object that holds the information about a specific date: 2008-04-30T00:00:00Z
When using the calendar.getTime() method I can get different results because I know that that method is looking for the local value
Thus:
UK: 2008-04-30T01:00:00.000+0100
US: 2008-04-30T20:00:00.000-0400
But I would like to get a Date object that holds just the Date and Time values "2008-04-30T00:00:00" ignoring completely any timezone.
How can I do that?
As I mentioned before I tried to use
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
but I always end up with the same results.
Any help would be really appreciated
Cheers.
Found out that you can clear the Timezone by using code below:
Calendar cal = Calendar.getInstance();
cal.clear(Calendar.ZONE_OFFSET);
Calendars and Dates mean nothing without a TimeZone.
Calendars and dates cannot exist without a timezone.
You can't ignore completely any timezone.
You can create a Calendar for Greenwich Mean Time (offset zero) like this:
TimeZone zone = TimeZone.getTimeZone("Etc/GMT");
Calendar cal = Calendar.getInstance(zone);
This represents a Date/Calendar that is only meaningful in the GMT timezone.
It sounds like you want a timestamp, which represents an instant in time.
As others have pointed out, Calendar and Date objects cannot exist without a time zone.
I believe you may want to use the LocalDateTime class introduced in Java 8 with the new time API:
LocalDateTime literal = LocalDateTime.of(2008, 4, 30, 0, 0, 0);
LocalDateTime parsed = LocalDateTime.parse("2008-04-30T00:00:00"); // ISO-8601 by default
Assert.assertEquals(literal, parsed);
Do you use a standard constructor for initializing Calendar? What if you used the constructor which allows to specify the time zone and locale?
protected Calendar(TimeZone zone, Locale aLocale)
Old, but still incorrect.
"When using the calendar.getTime() method I can get different results because I know that that method is looking for the local value"
That is a misconception. getTime() will get the Milliseconds only. Countet as GMT.
ONLY during formatting of the Output the time zone becomes relevant. Sind the original poster did not show the code, it can not be decided, where the error occurs.

Want only date from epoch

Given millis since epoch, I want an instance with the fields Month, Date, Year filled out, with the hour minute seconds set to some default values.
What is an efficient way to do this?
I know that there are sql ways to do it but is there a way to do it in Java?
Just use:
new Calendar(new Date(msSinceEpoch));
where the ms is a long value.
Use either LocalDate or DateMidnight in the Joda-Time API. The differences are explained in the javadocs.
Note that in order to truncate a point in time (some millis since epoch) to a specific calendar day, you might want to specify when midnight happened, or else you'll end up with midnight in the system's timezone. For example, you might call the LocalDate(long, DateTimeZone) constructor instead of the LocalDate(long) constructor.
Or, if you'd rather not have a JODA dependency, use DateFormat:
Date thisDate = new SimpleDateFormat("yyyy-MMM-dd").parse("2011-Jun-29");
Per the javadocs, you can easily create a Date from a long:
long value = System.currentTimeMillis();
Date thisDate = new Date(value);

Create jodatime LocalDate from java.sql.Time

I'm new to joda-time and I didn't find anywhere examples to do some simple things.
I want to make an object where to save a time value read from a table in a database (a java.sql.Time - e.g. "18:30:00") I don't care about time zone, so I think that I need LocalDate. But the problem is that I couldn't create a LocalDate object based on that Time object.
I tried with no success LocalDate.fromDateFields(), DateTimeParser.parseInto(), DateTimeParser.parseDateTime().
EDIT:
I should have used LocalTime.
These work:
java.sql.Time time = Time.valueOf("18:30:00");
LocalTime lt1 = LocalTime.fromDateFields(time);
LocalTime lt2 = new LocalTime(time);
According to the documentation, you should be able to construct a LocalDate directly by passing it a java.util.Date as the sole constructor argument. Since a java.sql.Time extends java.util.Date, you should be able to
final LocalDate ld = new LocalDate(mySqlTime);
This works for me:
System.out.println(new LocalDate(Time.valueOf("18:30:00")));
On the other hand, it's not a meaningful thing to do, since you'll always get January 1, 1970. But I imagine you know what you're doing.

Categories