Returning Calendar with SimpleDateFormat - java

I want to return the value of the calendar + 14 months using the SimpleDateFormat but am getting the below error.
private fun bestBeforeDate(cal: Calendar = Calendar.getInstance()): String
{
cal.add(Calendar.MONTH, 14)
val format1 = SimpleDateFormat("dd-MM-yyy", Locale.getDefault()).format(this)
return getString(R.string.best_before_date) + format1.format(cal)
}
java.lang.IllegalArgumentException: Cannot format given Object as a Date

java.time and ThreeTenABP
Sorry, I can write this in Java only. Please translate yourself. Your task is best solved using java.time, the modern Java date and time API.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
LocalDate date = LocalDate.now(ZoneId.of("Europe/London")).plusMonths(14);
String result = date.format(dateFormatter);
System.out.println("Result: " + result);
Output when running today:
Result: 22-12-2020
Fixing your code
If you insist on using the notoriously troublesome and long outdated SimpleDateFormat class, just remove .format(this) from your code. I bet the exception is coming from there, and it’s wrong since you have an almost correct call to the format method in the following line.
private fun bestBeforeDate(cal: Calendar = Calendar.getInstance()): String
{
cal.add(Calendar.MONTH, 14)
val format1 = SimpleDateFormat("dd-MM-yyyy")
return getString(R.string.best_before_date) + format1.format(cal.time)
}
The format method expects either a Date (another poorly designed and long outdated class) or a Long. Since this was neither of those, you got the exception.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

import java.util.Calendar;
import java.util.TimeZone;
/**
* Java program to add, subtract dates, month and year using Calendar in Java.
* Apart from date, Calendar class also provide time related information and can
* be used to add and subtract hours, minutes and seconds from time in Java.
*
* #author Shahzad Ali
*/
public class DateAndTimeArithmetic {
public static void main(String args[]){
//Java calendar in default timezone and default locale
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("current date: " + getDate(cal));
//adding days into Date in Java
cal.add(Calendar.DATE, 2);
System.out.println("date after 2 days : " + getDate(cal));
//subtracting days from Date in Java
cal.add(Calendar.DATE, -2);
System.out.println("date before 2 days : " + getDate(cal));
//adding moths into Date
cal.add(Calendar.MONTH, 5);
System.out.println("date after 5 months : " + getDate(cal));
//subtracting months from Date
cal.add(Calendar.MONTH, -5);
System.out.println("date before 5 months : " + getDate(cal));
//adding year into Date
cal.add(Calendar.YEAR, 5);
System.out.println("date after 5 years : " + getDate(cal));
//subtracting year from Date
cal.add(Calendar.YEAR, -5);
System.out.println("date before 5 years : " + getDate(cal));
//date after 200 days from now, takes care of how many days are in month
//for years calendar takes care of leap year as well
cal.add(Calendar.DATE, 200);
System.out.println("date after 200 days from today : " + getDate(cal));
System.out.println("current time in GMT: " + getTime(cal));
//adding hours into Date
cal.add(Calendar.HOUR_OF_DAY, 3);
System.out.println("Time after 3 hours : " + getTime(cal));
//subtracting hours from Date time
cal.add(Calendar.HOUR_OF_DAY, -3);
System.out.println("Time before 3 hours : " + getTime(cal));
//adding minutes into Date time
cal.add(Calendar.MINUTE, 3);
System.out.println("Time after 3 minutes : " + getTime(cal));
//subtracting minutes from Date time
cal.add(Calendar.HOUR_OF_DAY, -3);
System.out.println("Time before 3 minuets : " + getTime(cal));
}
/**
*
* #return current Date from Calendar in dd/MM/yyyy format
* adding 1 into month because Calendar month starts from zero
*/
public static String getDate(Calendar cal){
return "" + cal.get(Calendar.DATE) +"/" +
(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.YEAR);
}
/**
*
* #return current Date from Calendar in HH:mm:SS format
*
* adding 1 into month because Calendar month starts from zero
*/
public static String getTime(Calendar cal){
return "" + cal.get(Calendar.HOUR_OF_DAY) +":" +
(cal.get(Calendar.MINUTE)) + ":" + cal.get(Calendar.SECOND);
}
}
Sample Output Should be like this:
current date: 22/10/2019
date after 2 days : 22/10/2019
date before 2 days : 24/10/2019
date after 5 months : 22/3/2020
etc...

Related

Calculate Years and Months between Two Dates in Android

I am trying to display a custom text result between two dates. Here is the code in my Fragment, which return two dates:
public class HomeFragment extends BaseFragment {
...
dashboardViewModel.productCategory.observe(this, data - > {
if (data != null && data.getData() != null) {
int totalLos = Integer.parseInt(data.getData().getLos());
Log.e("totalLOS", String.valueOf(totalLos));
explainDays(totalLos);
mBinding.los.setText("");
}
});
}
And here the code in BaseFragment which generate two dates:
public abstract class BaseFragment extends Fragment {
...
public void explainDays(int totalDays) {
Calendar calendar = Calendar.getInstance();
Date start = calendar.getTime();
calendar.add(Calendar.DATE, -totalDays);
Date end = calendar.getTime();
Log.e("startDate", String.valueOf(start));
Log.e("endDate", String.valueOf(end));
}
}
From the code above, I am getting these three logs:
E/totalLOS: 1233
E/startDate: Mon Jun 08 19:45:08 GMT+07:00 2020
E/endDate: Sun Jan 22 19:45:08 GMT+07:00 2017
How do I generate a response like for example 1 Year and 5 Months since 2007 between these two date results? the since 2007 needs to be extracted from endDate from Log above.
Any help will be much appreciated.
Thank you.
java.time
You can do it as follows:
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Define format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss O yyyy");
// Date/time strings
String strEndDate = "Mon Jun 08 19:45:08 GMT+07:00 2020";
String strStartDate = "Sun Jan 22 19:45:08 GMT+07:00 2017";
// Define ZoneOffset
ZoneOffset zoneOffset = ZoneOffset.ofHours(6);
// Parse date/time strings into OffsetDateTime
OffsetDateTime startDate = OffsetDateTime.parse(strStartDate, formatter).withOffsetSameLocal(zoneOffset);
OffsetDateTime endDate = OffsetDateTime.parse(strEndDate, formatter).withOffsetSameLocal(zoneOffset);
// Calculate period between `startDate` and `endDate`
Period period = Period.between(startDate.toLocalDate(), endDate.toLocalDate());
// Display result
System.out.println(
period.getYears() + " years and " + period.getMonths() + " months since " + startDate.getYear());
}
}
Output:
3 years and 4 months since 2017
Note: Instead of using the outdated java.util.Date and SimpleDateFormat, use the modern date/time API. Check this to learn more about it.
Note: The following content has been copied from How to get start time and end time of a day in another timezone in Android
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
But don't we have any other option apart from switching to ThreeTenBP
Library?
If you insisted, I suppose that a way through using Calendar, Date and SimpleDateFormat could be found. Those classes are poorly designed and long outdated, so with what I know and don’t know I would prefer ThreeTenABP.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
If you can extract the month,day and year from both dates you can use following solution to calculate the exact difference between two dates.
int sday=22 //int day of month here
int smonth=1 //int month here
int syear=2017 //int year here
int eday=5 //int day of month here
int emonth=6 //int month here
int eyear=2020 //int year here
resyear = eyear - syear;
if (emonth >= smonth) {
resmonth = emonth - smonth;
} else {
resmonth = emonth - smonth;
resmonth = 12 + resmonth;
resyear--;
}
if (eday >= sday) {
resday = eday - sday;
} else {
resday = eday - sday;
resday = 31 + resday;
if (resmonth == 0) {
resmonth = 11;
resyear--;
} else {
resmonth--;
}
}
if (resday <0 || resmonth<0 || resyear<0) {
Toast.makeText(getApplicationContext(), "starting date must greater than ending date", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "difference: " + resyear + " years /" + resmonth + " months/" + resday + " days", Toast.LENGTH_LONG).show();
}
}

Get Start day and End day of a Week? [Java]

I am developing an app that constantly monitors the user's physical activity and inactivity levels. I am trying to figure out out to get the starting and ending day of a week when a date is provided. For example, 3 Mar is the date that I am providing and I want to get the starting and ending day of this week -> 27 Feb - 5 Mar. Is it possible to do that?
I am trying to achieve the following design
The following code that I currently have just concatenates the last and first date of the list of activities (one for every day is created).
private String formatDate(List<Activity> activities) {
Calendar calendar = Calendar.getInstance(Locale.UK);
Date date = activities.get(activities.size() - 1).getDate();
calendar.setTime(date);
String output = "" + calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.UK);
calendar.setTime(activities.get(0).getDate());
output += " - " + calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.UK);
return output;
}
Note: I have to mention that the List as a parameter are all of the activities grouped per week already
However, with this approach it becomes problematic when the person is not using the app (i.e. not logged in -> the app stops monitoring) and the text label could look something like that
(e.g. only one activity for this week)
Any advice please?
It is as simple as doing:
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
What is considered the first day of the week depends on the Locale used.
To get the last day of the week then do:
calendar.add(Calendar.DAY_OF_YEAR, 6);
java.time
The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat etc.) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.
Solution using java.time, the modern API:
For ISO 8601 week (Monday to Sunday), you can use ChronoField.DAY_OF_WEEK as 1 for the first day of the week and as 7 for the last day of the week.
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2017, Month.MARCH, 3);
LocalDate firstDayOfTheWeek = date.with(ChronoField.DAY_OF_WEEK, 1);
System.out.println(firstDayOfTheWeek); // 2017-02-27
LocalDate lastDayOfTheWeek = date.with(ChronoField.DAY_OF_WEEK, 7);
System.out.println(lastDayOfTheWeek); // 2017-03-05
}
}
Alternatively,
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.WeekFields;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2017, Month.MARCH, 3);
LocalDate firstDayOfTheWeek = date.with(WeekFields.ISO.getFirstDayOfWeek());
System.out.println(firstDayOfTheWeek); // 2017-02-27
LocalDate lastDayOfTheWeek = firstDayOfTheWeek.plusDays(6);
System.out.println(lastDayOfTheWeek); // 2017-03-05
}
}
Use WeekFields#of(Locale locale) to get the Locale-specific result (thanks, Ole V.V. for the suggestion):
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.WeekFields;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2017, Month.MARCH, 3);
System.out.println("France:");
LocalDate firstDayOfTheWeek = date.with(WeekFields.of(Locale.FRANCE).getFirstDayOfWeek());
System.out.println(firstDayOfTheWeek);
LocalDate lastDayOfTheWeek = firstDayOfTheWeek.plusDays(6);
System.out.println(lastDayOfTheWeek);
System.out.println();
System.out.println("USA:");
firstDayOfTheWeek = date.with(WeekFields.of(Locale.US).getFirstDayOfWeek());
System.out.println(firstDayOfTheWeek);
lastDayOfTheWeek = firstDayOfTheWeek.plusDays(6);
System.out.println(lastDayOfTheWeek);
}
}
Output:
France:
2017-02-27
2017-03-05
USA:
2017-03-05
2017-03-11
The documentation of WeekFields.ISO.getFirstDayOfWeek() says:
Gets the first day-of-week.
The first day-of-week varies by culture. For example, the US uses
Sunday, while France and the ISO-8601 standard use Monday. This method
returns the first day using the standard DayOfWeek enum.
Learn more about java.time, the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
You just need to use the Calendar.DAY_OF_WEEK parameter to work out how many days to subtract - for example to print the start and end of the current week:
public static void main(String[] args) {
Calendar calendar = GregorianCalendar.getInstance();
// Subtract number of days to start of week
calendar.add(Calendar.DATE, -(calendar.get(Calendar.DAY_OF_WEEK)-1));
String output = "" + calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.UK);
calendar.add(Calendar.DATE, 6);
output += " - " + calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.UK);
System.out.println(output);
}
Thanks to #BarrySW19 and #john16384, here is the working method:
private String formatDate(List<Activity> activities) {
Calendar calendar = Calendar.getInstance(Locale.UK);
Date date = activities.get(activities.size() - 1).getDate();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
String output = "" + calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.UK);
calendar.add(Calendar.DAY_OF_YEAR, 6);
output += " - " + calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.UK);
return output;
}

Java: check if a given date is within current month

I need to check if a given date falls in the current month, and I wrote the following code, but the IDE reminded me that the getMonth() and getYear() methods are obsolete. I was wondering how to do the same thing in newer Java 7 or Java 8.
private boolean inCurrentMonth(Date givenDate) {
Date today = new Date();
return givenDate.getMonth() == today.getMonth() && givenDate.getYear() == today.getYear();
}
//Create 2 instances of Calendar
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
//set the given date in one of the instance and current date in the other
cal1.setTime(givenDate);
cal2.setTime(new Date());
//now compare the dates using methods on Calendar
if(cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) {
if(cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) {
// the date falls in current month
}
}
java.time (Java 8)
There are several ways to do it with the new java.time API (tutorial). You can do it using .get(ChronoField.XY), but I think this is prettier:
Instant given = givenDate.toInstant();
Instant ref = Instant.now();
return Month.from(given) == Month.from(ref) && Year.from(given).equals(Year.from(ref));
For better re-usability you can also refactor this code to "temporal query":
public class TemporalQueries {
//TemporalQuery<R> { R queryFrom(TemporalAccessor temporal) }
public static Boolean isCurrentMonth(TemporalAccessor temporal) {
Instant ref = Instant.now();
return Month.from(temporal) == Month.from(ref) && Year.from(temporal).equals(Year.from(ref));
}
}
Boolean result = givenDate.toInstant().query(TemporalQueries::isCurrentMonth); //Lambda using method reference
Time Zone
The other answers ignore the crucial issue of time zone. A new day dawns earlier in Paris than in Montréal. So at the same simultaneous moment, the dates are different, "tomorrow" in Paris while "yesterday" in Montréal.
Joda-Time
The java.util.Date and .Calendar classes bundled with Java are notoriously troublesome, confusing, and flawed. Avoid them.
Instead use either Joda-Time library or the java.time package in Java 8 (inspired by Joda-Time).
Here is example code in Joda-Time 2.5.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime dateTime = new DateTime( yourJUDate, zone ); // Convert java.util.Date to Joda-Time, and assign time zone to adjust.
DateTime now = DateTime.now( zone );
// Now see if the month and year match.
if ( ( dateTime.getMonthOfYear() == now.getMonthOfYear() ) && ( dateTime.getYear() == now.getYear() ) ) {
// You have a hit.
}
For a more general solution to see if a moment falls within any span of time (not just a month), search StackOverflow for "joda" and "interval" and "contain".
java.time (Java 8)
Java 8 provides the YearMonth class which represents a given month within a given year (e.g. January 2018). This can be used to compare against the YearMonth of the given date.
private boolean inCurrentMonth(Date givenDate) {
ZoneId timeZone = ZoneOffset.UTC; // Use whichever time zone makes sense for your use case
LocalDateTime givenLocalDateTime = LocalDateTime.ofInstant(givenDate.toInstant(), timeZone);
YearMonth currentMonth = YearMonth.now(timeZone);
return currentMonth.equals(YearMonth.from(givenLocalDateTime));
}
Note that this approach will work for any of the Java 8 time classes that have both a month and a date part (LocalDate, ZonedDateTime, etc.) and not just LocalDateTime.
As far as I know the Calendar class and all derived from it return the date using the get(). See the documentation for this class. Also here is an example taken from here:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
Calendar calendar = new GregorianCalendar(2013,1,28,13,24,56);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);
int hour = calendar.get(Calendar.HOUR); // 12 hour clock
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);
System.out.println(sdf.format(calendar.getTime()));
System.out.println("year \t\t: " + year);
System.out.println("month \t\t: " + month);
System.out.println("dayOfMonth \t: " + dayOfMonth);
System.out.println("dayOfWeek \t: " + dayOfWeek);
System.out.println("weekOfYear \t: " + weekOfYear);
System.out.println("weekOfMonth \t: " + weekOfMonth);
System.out.println("hour \t\t: " + hour);
System.out.println("hourOfDay \t: " + hourOfDay);
System.out.println("minute \t\t: " + minute);
System.out.println("second \t\t: " + second);
System.out.println("millisecond \t: " + millisecond);
which outputs
2013 Feb 28 13:24:56
year : 2013
month : 1
dayOfMonth : 28
dayOfWeek : 5
weekOfYear : 9
weekOfMonth : 5
hour : 1
hourOfDay : 13
minute : 24
second : 56
millisecond : 0
I think it was replaced because the new way offers a much simpler handling using a single function, which is much easier to remember.

Retrieve current week's Monday's date

We have a utility that will run any day between Monday - Friday. It will update some number of files inside a Content Management Tool. The last modified date associated with that file should be, that week's monday's date. I wrote the following program to retrieve current week's monday's date. But I am still not sure whether this would work for all scenarios. Has anyone got a better solution ?
Calendar c = Calendar.getInstance();
c.setTime(new Date());
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.DAY_OF_WEEK));
int mondayNo = c.get(Calendar.DAY_OF_MONTH)-c.get(Calendar.DAY_OF_WEEK)+2;
c.set(Calendar.DAY_OF_MONTH,mondayNo);
System.out.println("Date "+c.getTime());
I would strongly recommend using Joda Time instead (for all your date/time work, not just this):
// TODO: Consider time zones, calendars etc
LocalDate now = new LocalDate();
LocalDate monday = now.withDayOfWeek(DateTimeConstants.MONDAY);
System.out.println(monday);
Note that as you've used Monday here, which is the first day of the week in Joda Time, this will always return an earlier day (or the same day). If you chosen Wednesday (for example), then it would advance to Wednesday from Monday or Tuesday. You can always add or subtract a week if you need "the next Wednesday" or "the previous Wednesday".
EDIT: If you really want to use java.util.Date/Calendar, you can use:
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Date " + c.getTime());
You can use Calendar.setFirstDayOfWeek to indicate whether a week is Monday-Sunday or Sunday-Saturday; I believe setting the day of the week will stay within the current week - but test it.
tl;dr
LocalDate previousMonday =
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.with( TemporalAdjusters.previous( DayOfWeek.MONDAY ) );
java.time
Both the java.util.Calendar class and the Joda-Time library have been supplanted by the java.time framework built into Java 8 and later. See Oracle Tutorial. Much of the java.time functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP. With modern Android tooling and its "API desugaring", you need not add any library.
The java.time.LocalDate class represents a date-only value without time-of-day and without time zone.
Determining today's date requires a time zone, a ZoneId.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
The TemporalAdjuster interface (see Tutorial) is a powerful but simple way to manipulate date-time values. The TemporalAdjusters class (note the plural) implements some very useful adjustments. Here we use previous( DayOfWeek).
The handy DayOfWeek enum makes it easy to specify a day-of-week.
LocalDate previousMonday = today.with( TemporalAdjusters.previous( DayOfWeek.MONDAY ) );
If today is Monday, and you want to use today rather than a week ago, call previousOrSame.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
Java 9 brought some minor features and fixes.
Java SE 6 and Java SE 7
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android (26+) bundle implementations of the java.time classes.
For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
The following will work, including wrapping months:
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(new Date());
int today = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DAY_OF_WEEK, -today+Calendar.MONDAY);
System.out.println("Date "+c.getTime());
If, however, you edit your application on a Sunday (eg. Sunday 12 Feb), the date will be for the following Monday. Based on your requirements (the app will only run Monday thru Friday), this should not pose a problem.
As Jon suggested, the calendar.set method works...
I've tested it both in the case of a monday in same month and in another month using following snippet :
Calendar c = Calendar.getInstance();
//ensure the method works within current month
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Date " + c.getTime());
//go to the 1st week of february, in which monday was in january
c.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("Date " + c.getTime());
//test that setting day_of_week to monday gives a date in january
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Date " + c.getTime());
//same for tuesday
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println("Date " + c.getTime());
The results:
Date Mon Feb 13 10:29:41 CET 2012
Date Wed Feb 01 10:29:41 CET 2012
Date Mon Jan 30 10:29:41 CET 2012
Date Tue Jan 31 10:29:41 CET 2012
What about using Joda Time library... Take look at this answer...
in case you don't want to use Joda Time you can do like this to find the weeks -> (works on Android)
public static ArrayList<String> getWeeks(int month) {
ArrayList<String> arrayListValues = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
int day = calendar.get(Calendar.DAY_OF_WEEK);
while (day != 1) {
calendar.add(Calendar.DATE, 1);
day = calendar.get(Calendar.DAY_OF_WEEK);
}
calendar.add(Calendar.DATE, -14);
String y1 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 6);
String y2 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 1);
arrayListValues.add(y1 + " - " + y2);
y1 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 6);
y2 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 1);
arrayListValues.add(y1 + " - " + y2);
for (int i = 0; i < 10; i++) {
y1 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 6);
y2 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 1);
arrayListValues.add(y1 + " - " + y2);
}
return arrayListValues;
}
For version Android 6.0 or grater use:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
int day = 1, month = 7, year = 2018;
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(year, month, day);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //change de day to monday
String dateMonday= sdf.format(c.getTime());
For version Android 5.1 or less it does not work appropriately, I created my own method.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
int day = 1, month = 7, year = 2018;
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
//set the date with your date or the current date c.set(new Date());
c.set(year, month, day);
int diaSemana = c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i < 7; i++) {
if (diaSemana != Calendar.MONDAY) {
day--;
c.set(year, month, day);
diaSemana = c.get(Calendar.DAY_OF_WEEK);
if (diaSemana == Calendar.MONDAY) break;
} else {
break;
}
}
String dateMonday= sdf.format(c.getTime());
Kotlin code. Works in older android versions
val c = Calendar.getInstance()
c.time = Date()
while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
c.add(Calendar.DAY_OF_WEEK, -1)
}
It won't work if the currents week monday is the month before... For example if today is Friday 1st of June... You should probably rather use the roll method...

Changing Java Date one hour back

I have a Java date object:
Date currentDate = new Date();
This will give the current date and time. Example:
Thu Jan 12 10:17:47 GMT 2012
Instead, I want to get the date, changing it to one hour back so it should give me:
Thu Jan 12 09:17:47 GMT 2012
What would be the best way to do it?
java.util.Calendar
Calendar cal = Calendar.getInstance();
// remove next line if you're always using the current time.
cal.setTime(currentDate);
cal.add(Calendar.HOUR, -1);
Date oneHourBack = cal.getTime();
java.util.Date
new Date(System.currentTimeMillis() - 3600 * 1000);
org.joda.time.LocalDateTime
new LocalDateTime().minusHours(1)
Java 8: java.time.LocalDateTime
LocalDateTime.now().minusHours(1)
Java 8 java.time.Instant
// always in UTC if not timezone set
Instant.now().minus(1, ChronoUnit.HOURS));
// with timezone, Europe/Berlin for example
Instant.now()
.atZone(ZoneId.of("Europe/Berlin"))
.minusHours(1));
Similar to #Sumit Jain's solution
Date currentDate = new Date(System.currentTimeMillis() - 3600 * 1000);
or
Date currentDate = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1));
tl;dr
In UTC:
Instant.now().minus( 1 , ChronoUnit.HOURS )
Or, zoned:
Instant.now()
.atZone( ZoneId.of ( "America/Montreal" ) )
.minusHours( 1 )
Using java.time
Java 8 and later has the new java.time framework built-in.
Instant
If you only care about UTC (GMT), then use the Instant class.
Instant instant = Instant.now ();
Instant instantHourEarlier = instant.minus ( 1 , ChronoUnit.HOURS );
Dump to console.
System.out.println ( "instant: " + instant + " | instantHourEarlier: " + instantHourEarlier );
instant: 2015-10-29T00:37:48.921Z | instantHourEarlier: 2015-10-28T23:37:48.921Z
Note how in this instant happened to skip back to yesterday’s date.
ZonedDateTime
If you care about a time zone, use the ZonedDateTime class. You can start with an Instant and the assign a time zone, a ZoneId object. This class handles the necessary adjustments for anomalies such as Daylight Saving Time (DST).
Instant instant = Instant.now ();
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
ZonedDateTime zdtHourEarlier = zdt.minus ( 1 , ChronoUnit.HOURS );
Dump to console.
System.out.println ( "instant: " + instant + "\nzdt: " + zdt + "\nzdtHourEarlier: " + zdtHourEarlier );
instant: 2015-10-29T00:50:30.778Z
zdt: 2015-10-28T20:50:30.778-04:00[America/Montreal]
zdtHourEarlier: 2015-10-28T19:50:30.778-04:00[America/Montreal]
Conversion
The old java.util.Date/.Calendar classes are now outmoded. Avoid them. They are notoriously troublesome and confusing.
When you must use the old classes for operating with old code not yet updated for the java.time types, call the conversion methods. Here is example code going from an Instant or a ZonedDateTime to a java.util.Date.
java.util.Date date = java.util.Date.from( instant );
…or…
java.util.Date date = java.util.Date.from( zdt.toInstant() );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
Use Calendar.
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - 1);
Or using the famous Joda Time library:
DateTime dateTime = new DateTime();
dateTime = dateTime.minusHours(1);
Date modifiedDate = dateTime.toDate();
Just subtract the number of milliseconds in an hour from the date.
currentDate.setTime(currentDate.getTime() - 3600 * 1000));
You can use from bellow code for date and time :
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, 1);
System.out.println("Add one day to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
System.out.println("Add one month to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
System.out.println("Add one year to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
System.out.println("Add one hour to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1);
System.out.println("Add one minute to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 1);
System.out.println("Add one second to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println("Subtract one day from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
System.out.println("Subtract one month from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
System.out.println("Subtract one year from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);
System.out.println("Subtract one hour from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -1);
System.out.println("Subtract one minute from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -1);
System.out.println("Subtract one second from current date : " + dateFormat.format(cal.getTime()));
Output :
Current Date Time : 2008/12/28 10:24:53
Add one day to current date : 2008/12/29 10:24:53
Add one month to current date : 2009/01/28 10:24:53
Add one year to current date : 2009/12/28 10:24:53
Add one hour to current date : 2008/12/28 11:24:53
Add one minute to current date : 2008/12/28 10:25:53
Add one second to current date : 2008/12/28 10:24:54
Subtract one day from current date : 2008/12/27 10:24:53
Subtract one month from current date : 2008/11/28 10:24:53
Subtract one year from current date : 2007/12/28 10:24:53
Subtract one hour from current date : 2008/12/28 09:24:53
Subtract one minute from current date : 2008/12/28 10:23:53
Subtract one second from current date : 2008/12/28 10:24:52
This link is good : See here
And see : See too
And : Here
And : Here
And : Here
If you need just time :
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
This can be achieved using java.util.Date. The following code will subtract 1 hour from your date.
Date date = new Date(yourdate in date format);
Date newDate = DateUtils.addHours(date, -1)
Similarly for subtracting 20 seconds from your date
newDate = DateUtils.addSeconds(date, -20)
To subtract hours, you need to use the HOUR_OF_DAY constant. Within that, include the number with the negative sign. This would be the hours you want to reduce. All this is done under the Calendar add() method.
The following is an example:
import java.util.Calendar;
public class Example {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println("Date : " + c.getTime());
// 2 hours subtracted
c.add(Calendar.HOUR_OF_DAY, -2);
System.out.println("After subtracting 2 hrs : " + c.getTime());
}
}
Here is the output:
Date : Sun Dec 16 16:28:53 UTC 2018
After subtracting 2 hrs : Sun Dec 16 14:28:53 UTC 2018
Get the time in milliseconds, minus your minutes in milliseconds and convert it to Date. Here you need to objectify one!!!
int minutes = 60;
long currentDateTime = System.currentTimeMillis();
Date currentDate = new Date(currentDateTime - minutes*60*1000);
System.out.println(currentDate);
It worked for me instead using format .To work with time just use parse and toString() methods
String localTime="6:11";
LocalTime localTime = LocalTime.parse(localtime)
LocalTime lt = 6:11;
localTime = lt.toString()

Categories