List all weeks,months and year in JAVA/Android
Is it possible to list the all weeks/date given two date range for example:
Date from 1/1/2013 to 1/1/2020
result will be:
- 1-7,2013
- 8-14,2013
- 15-21,2013 and soon til 2020 and same with month.
java
|
show 1 more comment
Is it possible to list the all weeks/date given two date range for example:
Date from 1/1/2013 to 1/1/2020
result will be:
- 1-7,2013
- 8-14,2013
- 15-21,2013 and soon til 2020 and same with month.
java
4
sure, go for it. Let us know of any errors / problems you encounter
– Scary Wombat
Nov 20 '18 at 2:54
Honestly, i have no idea on how to do this part, still searching for a reference similar to my problem.
– Hard Harry Nadela
Nov 20 '18 at 2:58
well weeks, months, year sounds like a calendar to me. Hmm maybe search forjava calendar?
– Scary Wombat
Nov 20 '18 at 2:59
Ahh okay okay, i also search similar like calendar but i have no idea upon getting the dates and range of the weeks, like 1-7,2013 and soon.
– Hard Harry Nadela
Nov 20 '18 at 3:03
Reference: Oracle Tutorial: Date Time
– Ole V.V.
Nov 20 '18 at 9:43
|
show 1 more comment
Is it possible to list the all weeks/date given two date range for example:
Date from 1/1/2013 to 1/1/2020
result will be:
- 1-7,2013
- 8-14,2013
- 15-21,2013 and soon til 2020 and same with month.
java
Is it possible to list the all weeks/date given two date range for example:
Date from 1/1/2013 to 1/1/2020
result will be:
- 1-7,2013
- 8-14,2013
- 15-21,2013 and soon til 2020 and same with month.
java
java
edited Nov 20 '18 at 5:55
cricket_007
80.2k1142110
80.2k1142110
asked Nov 20 '18 at 2:53
Hard Harry NadelaHard Harry Nadela
41
41
4
sure, go for it. Let us know of any errors / problems you encounter
– Scary Wombat
Nov 20 '18 at 2:54
Honestly, i have no idea on how to do this part, still searching for a reference similar to my problem.
– Hard Harry Nadela
Nov 20 '18 at 2:58
well weeks, months, year sounds like a calendar to me. Hmm maybe search forjava calendar?
– Scary Wombat
Nov 20 '18 at 2:59
Ahh okay okay, i also search similar like calendar but i have no idea upon getting the dates and range of the weeks, like 1-7,2013 and soon.
– Hard Harry Nadela
Nov 20 '18 at 3:03
Reference: Oracle Tutorial: Date Time
– Ole V.V.
Nov 20 '18 at 9:43
|
show 1 more comment
4
sure, go for it. Let us know of any errors / problems you encounter
– Scary Wombat
Nov 20 '18 at 2:54
Honestly, i have no idea on how to do this part, still searching for a reference similar to my problem.
– Hard Harry Nadela
Nov 20 '18 at 2:58
well weeks, months, year sounds like a calendar to me. Hmm maybe search forjava calendar?
– Scary Wombat
Nov 20 '18 at 2:59
Ahh okay okay, i also search similar like calendar but i have no idea upon getting the dates and range of the weeks, like 1-7,2013 and soon.
– Hard Harry Nadela
Nov 20 '18 at 3:03
Reference: Oracle Tutorial: Date Time
– Ole V.V.
Nov 20 '18 at 9:43
4
4
sure, go for it. Let us know of any errors / problems you encounter
– Scary Wombat
Nov 20 '18 at 2:54
sure, go for it. Let us know of any errors / problems you encounter
– Scary Wombat
Nov 20 '18 at 2:54
Honestly, i have no idea on how to do this part, still searching for a reference similar to my problem.
– Hard Harry Nadela
Nov 20 '18 at 2:58
Honestly, i have no idea on how to do this part, still searching for a reference similar to my problem.
– Hard Harry Nadela
Nov 20 '18 at 2:58
well weeks, months, year sounds like a calendar to me. Hmm maybe search for
java calendar ?– Scary Wombat
Nov 20 '18 at 2:59
well weeks, months, year sounds like a calendar to me. Hmm maybe search for
java calendar ?– Scary Wombat
Nov 20 '18 at 2:59
Ahh okay okay, i also search similar like calendar but i have no idea upon getting the dates and range of the weeks, like 1-7,2013 and soon.
– Hard Harry Nadela
Nov 20 '18 at 3:03
Ahh okay okay, i also search similar like calendar but i have no idea upon getting the dates and range of the weeks, like 1-7,2013 and soon.
– Hard Harry Nadela
Nov 20 '18 at 3:03
Reference: Oracle Tutorial: Date Time
– Ole V.V.
Nov 20 '18 at 9:43
Reference: Oracle Tutorial: Date Time
– Ole V.V.
Nov 20 '18 at 9:43
|
show 1 more comment
2 Answers
2
active
oldest
votes
Please try out this for the case of weeks(check if you can optimize).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
You are using the badly designed and long outdated date and time classes (Calendar,Date,SimpleDateFormatandDateFormat). Today we have so much better injava.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.
– Ole V.V.
Nov 20 '18 at 9:49
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
add a comment |
java.time
I would use a LocalDate for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.). Happy coding. And as Scary Wombat already said, if you have any problems, please ask a different and better question here.
Question: Can I use java.time on Android?
Yes, java.time works nicely on Android devices. It just requires at least Java 6.
- In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
- On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package
org.threeten.bpand subpackages.
Links
Oracle tutorial: Date Time, explaining how to usejava.time.- Documentation of
LocalDate,ChronoUnitandPeriod. - ThreeTen Backport project
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Java Specification Request (JSR) 310.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385535%2flist-all-weeks-months-and-year-in-java-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please try out this for the case of weeks(check if you can optimize).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
You are using the badly designed and long outdated date and time classes (Calendar,Date,SimpleDateFormatandDateFormat). Today we have so much better injava.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.
– Ole V.V.
Nov 20 '18 at 9:49
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
add a comment |
Please try out this for the case of weeks(check if you can optimize).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
You are using the badly designed and long outdated date and time classes (Calendar,Date,SimpleDateFormatandDateFormat). Today we have so much better injava.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.
– Ole V.V.
Nov 20 '18 at 9:49
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
add a comment |
Please try out this for the case of weeks(check if you can optimize).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
Please try out this for the case of weeks(check if you can optimize).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
edited Nov 20 '18 at 6:00
answered Nov 20 '18 at 5:54
Pooja Rajendran CPooja Rajendran C
197210
197210
You are using the badly designed and long outdated date and time classes (Calendar,Date,SimpleDateFormatandDateFormat). Today we have so much better injava.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.
– Ole V.V.
Nov 20 '18 at 9:49
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
add a comment |
You are using the badly designed and long outdated date and time classes (Calendar,Date,SimpleDateFormatandDateFormat). Today we have so much better injava.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.
– Ole V.V.
Nov 20 '18 at 9:49
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
You are using the badly designed and long outdated date and time classes (
Calendar, Date, SimpleDateFormat and DateFormat). Today we have so much better in java.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.– Ole V.V.
Nov 20 '18 at 9:49
You are using the badly designed and long outdated date and time classes (
Calendar, Date, SimpleDateFormat and DateFormat). Today we have so much better in java.time, the modern Java date and time API. While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need ThreeTenABP.– Ole V.V.
Nov 20 '18 at 9:49
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V.
– Pooja Rajendran C
Nov 20 '18 at 10:18
add a comment |
java.time
I would use a LocalDate for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.). Happy coding. And as Scary Wombat already said, if you have any problems, please ask a different and better question here.
Question: Can I use java.time on Android?
Yes, java.time works nicely on Android devices. It just requires at least Java 6.
- In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
- On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package
org.threeten.bpand subpackages.
Links
Oracle tutorial: Date Time, explaining how to usejava.time.- Documentation of
LocalDate,ChronoUnitandPeriod. - ThreeTen Backport project
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Java Specification Request (JSR) 310.
add a comment |
java.time
I would use a LocalDate for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.). Happy coding. And as Scary Wombat already said, if you have any problems, please ask a different and better question here.
Question: Can I use java.time on Android?
Yes, java.time works nicely on Android devices. It just requires at least Java 6.
- In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
- On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package
org.threeten.bpand subpackages.
Links
Oracle tutorial: Date Time, explaining how to usejava.time.- Documentation of
LocalDate,ChronoUnitandPeriod. - ThreeTen Backport project
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Java Specification Request (JSR) 310.
add a comment |
java.time
I would use a LocalDate for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.). Happy coding. And as Scary Wombat already said, if you have any problems, please ask a different and better question here.
Question: Can I use java.time on Android?
Yes, java.time works nicely on Android devices. It just requires at least Java 6.
- In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
- On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package
org.threeten.bpand subpackages.
Links
Oracle tutorial: Date Time, explaining how to usejava.time.- Documentation of
LocalDate,ChronoUnitandPeriod. - ThreeTen Backport project
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Java Specification Request (JSR) 310.
java.time
I would use a LocalDate for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.). Happy coding. And as Scary Wombat already said, if you have any problems, please ask a different and better question here.
Question: Can I use java.time on Android?
Yes, java.time works nicely on Android devices. It just requires at least Java 6.
- In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
- On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package
org.threeten.bpand subpackages.
Links
Oracle tutorial: Date Time, explaining how to usejava.time.- Documentation of
LocalDate,ChronoUnitandPeriod. - ThreeTen Backport project
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Java Specification Request (JSR) 310.
edited Nov 20 '18 at 19:47
answered Nov 20 '18 at 11:06
Ole V.V.Ole V.V.
27.5k63152
27.5k63152
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385535%2flist-all-weeks-months-and-year-in-java-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

4
sure, go for it. Let us know of any errors / problems you encounter
– Scary Wombat
Nov 20 '18 at 2:54
Honestly, i have no idea on how to do this part, still searching for a reference similar to my problem.
– Hard Harry Nadela
Nov 20 '18 at 2:58
well weeks, months, year sounds like a calendar to me. Hmm maybe search for
java calendar?– Scary Wombat
Nov 20 '18 at 2:59
Ahh okay okay, i also search similar like calendar but i have no idea upon getting the dates and range of the weeks, like 1-7,2013 and soon.
– Hard Harry Nadela
Nov 20 '18 at 3:03
Reference: Oracle Tutorial: Date Time
– Ole V.V.
Nov 20 '18 at 9:43