Java - Create an array of times (15 minute) intervals between current time and a future set time
I'm trying to create an array of times from the current time to a set time, for example; the current time is 15:41, I would like that to be rounded up to the nearest quarter of an hour (15:45) and an array of 15 minute intervals to be created from 15:45 to a specified time lets say 23:30.
I've managed to create an array of times for a 24 hour period in 15 minute intervals and can't seem to get any further forward.
String quarterHours = {"00","15","30","45"};
String times = new String[24];
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times[i] = "Today " + time;
}
}
The output from the above in a DialogList view within Android looks as follows:
java
add a comment |
I'm trying to create an array of times from the current time to a set time, for example; the current time is 15:41, I would like that to be rounded up to the nearest quarter of an hour (15:45) and an array of 15 minute intervals to be created from 15:45 to a specified time lets say 23:30.
I've managed to create an array of times for a 24 hour period in 15 minute intervals and can't seem to get any further forward.
String quarterHours = {"00","15","30","45"};
String times = new String[24];
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times[i] = "Today " + time;
}
}
The output from the above in a DialogList view within Android looks as follows:
java
You're overwriting the time each cycle of the inner loop. You should use aList<String>
instead and just append without worrying about indexes.
– Federico klez Culloca
Nov 12 at 15:48
1
Thanks @K.Nicholas for the equation to round up to the nearest quarter hour. I will use this and combine with the marked answer to get my desired result. I didn't pay attention in maths class either, you're right.
– JamLis
Nov 12 at 16:12
add a comment |
I'm trying to create an array of times from the current time to a set time, for example; the current time is 15:41, I would like that to be rounded up to the nearest quarter of an hour (15:45) and an array of 15 minute intervals to be created from 15:45 to a specified time lets say 23:30.
I've managed to create an array of times for a 24 hour period in 15 minute intervals and can't seem to get any further forward.
String quarterHours = {"00","15","30","45"};
String times = new String[24];
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times[i] = "Today " + time;
}
}
The output from the above in a DialogList view within Android looks as follows:
java
I'm trying to create an array of times from the current time to a set time, for example; the current time is 15:41, I would like that to be rounded up to the nearest quarter of an hour (15:45) and an array of 15 minute intervals to be created from 15:45 to a specified time lets say 23:30.
I've managed to create an array of times for a 24 hour period in 15 minute intervals and can't seem to get any further forward.
String quarterHours = {"00","15","30","45"};
String times = new String[24];
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times[i] = "Today " + time;
}
}
The output from the above in a DialogList view within Android looks as follows:
java
java
asked Nov 12 at 15:45
JamLis
187113
187113
You're overwriting the time each cycle of the inner loop. You should use aList<String>
instead and just append without worrying about indexes.
– Federico klez Culloca
Nov 12 at 15:48
1
Thanks @K.Nicholas for the equation to round up to the nearest quarter hour. I will use this and combine with the marked answer to get my desired result. I didn't pay attention in maths class either, you're right.
– JamLis
Nov 12 at 16:12
add a comment |
You're overwriting the time each cycle of the inner loop. You should use aList<String>
instead and just append without worrying about indexes.
– Federico klez Culloca
Nov 12 at 15:48
1
Thanks @K.Nicholas for the equation to round up to the nearest quarter hour. I will use this and combine with the marked answer to get my desired result. I didn't pay attention in maths class either, you're right.
– JamLis
Nov 12 at 16:12
You're overwriting the time each cycle of the inner loop. You should use a
List<String>
instead and just append without worrying about indexes.– Federico klez Culloca
Nov 12 at 15:48
You're overwriting the time each cycle of the inner loop. You should use a
List<String>
instead and just append without worrying about indexes.– Federico klez Culloca
Nov 12 at 15:48
1
1
Thanks @K.Nicholas for the equation to round up to the nearest quarter hour. I will use this and combine with the marked answer to get my desired result. I didn't pay attention in maths class either, you're right.
– JamLis
Nov 12 at 16:12
Thanks @K.Nicholas for the equation to round up to the nearest quarter hour. I will use this and combine with the marked answer to get my desired result. I didn't pay attention in maths class either, you're right.
– JamLis
Nov 12 at 16:12
add a comment |
4 Answers
4
active
oldest
votes
You're overwriting the time each cycle of the inner loop. You should use a List<String>
instead and just append without worrying about indexes, like this:
String quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
add a comment |
It would be much easier to work with the built-in Date/Time API, specifically LocalTime
:
LocalTime time = LocalTime.now();
int minute = time.getMinute();
if (minute > 45) {
time = time.plusHours(1L).withMinute(0);
} else {
time = time.withMinute(minute < 30 ? minute < 15 ? 15 : 30: 45);
}
Stream.iterate(time.truncatedTo(ChronoUnit.MINUTES), t -> t.plusMinutes(15L))
.limit(5)
.forEach(System.out::println);
The above code outputs the following:
11:00
11:15
11:30
11:45
12:00
Your job will just be to figure out the math to limit the Stream
size.
add a comment |
Try this. Inserting items to array by times[i]
is incorrect in this scenario. It will only count the first loop items
public class Main
{
public static void main(String args)
{
String quarterHours = { "00", "15", "30", "45" };
String times = new String[96];
int count = 0;
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 4; j++)
{
String time = i + ":" + quarterHours[j];
if (i < 10)
{
time = "0" + time;
}
times[count] = "Today " + time;
count++;
}
}
for (int i = 0; i < times.length; i++)
{
System.out.println(times[i]);
}
}
}
add a comment |
Problem in the previous approach was that the last minutes-turn wins and overwrites any previous result.
I suggest using a List
, it is much easier to handle. You don't need to take care for the size.
String quarterHours = { "00", "15", "30", "45" };
List<String> times = new ArrayList<>();
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
times.add("Today " + time);
}
}
System.out.println(times);
Output:
[Today 00:00, Today 00:15, Today 00:30, Today 00:45, Today 01:00, Today 01:15, Today 01:30, Today 01:45, Today 02:00, Today 02:15, Today 02:30, Today 02:45, Today 03:00, Today 03:15, Today 03:30, Today 03:45, Today 04:00, Today 04:15, Today 04:30, Today 04:45, Today 05:00, Today 05:15, Today 05:30, Today 05:45, Today 06:00, Today 06:15, Today 06:30, Today 06:45, Today 07:00, Today 07:15, Today 07:30, Today 07:45, Today 08:00, Today 08:15, Today 08:30, Today 08:45, Today 09:00, Today 09:15, Today 09:30, Today 09:45, Today 10:00, Today 10:15, Today 10:30, Today 10:45, Today 11:00, Today 11:15, Today 11:30, Today 11:45, Today 12:00, Today 12:15, Today 12:30, Today 12:45, Today 13:00, Today 13:15, Today 13:30, Today 13:45, Today 14:00, Today 14:15, Today 14:30, Today 14:45, Today 15:00, Today 15:15, Today 15:30, Today 15:45, Today 16:00, Today 16:15, Today 16:30, Today 16:45, Today 17:00, Today 17:15, Today 17:30, Today 17:45, Today 18:00, Today 18:15, Today 18:30, Today 18:45, Today 19:00, Today 19:15, Today 19:30, Today 19:45, Today 20:00, Today 20:15, Today 20:30, Today 20:45, Today 21:00, Today 21:15, Today 21:30, Today 21:45, Today 22:00, Today 22:15, Today 22:30, Today 22:45, Today 23:00, Today 23:15, Today 23:30, Today 23:45]
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%2f53265558%2fjava-create-an-array-of-times-15-minute-intervals-between-current-time-and-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're overwriting the time each cycle of the inner loop. You should use a List<String>
instead and just append without worrying about indexes, like this:
String quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
add a comment |
You're overwriting the time each cycle of the inner loop. You should use a List<String>
instead and just append without worrying about indexes, like this:
String quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
add a comment |
You're overwriting the time each cycle of the inner loop. You should use a List<String>
instead and just append without worrying about indexes, like this:
String quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}
You're overwriting the time each cycle of the inner loop. You should use a List<String>
instead and just append without worrying about indexes, like this:
String quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}
answered Nov 12 at 15:53
Federico klez Culloca
15.6k134275
15.6k134275
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
add a comment |
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
Thanks for this, much easier to change my starting time now without throwing array out of range errors.
– JamLis
Nov 12 at 16:11
add a comment |
It would be much easier to work with the built-in Date/Time API, specifically LocalTime
:
LocalTime time = LocalTime.now();
int minute = time.getMinute();
if (minute > 45) {
time = time.plusHours(1L).withMinute(0);
} else {
time = time.withMinute(minute < 30 ? minute < 15 ? 15 : 30: 45);
}
Stream.iterate(time.truncatedTo(ChronoUnit.MINUTES), t -> t.plusMinutes(15L))
.limit(5)
.forEach(System.out::println);
The above code outputs the following:
11:00
11:15
11:30
11:45
12:00
Your job will just be to figure out the math to limit the Stream
size.
add a comment |
It would be much easier to work with the built-in Date/Time API, specifically LocalTime
:
LocalTime time = LocalTime.now();
int minute = time.getMinute();
if (minute > 45) {
time = time.plusHours(1L).withMinute(0);
} else {
time = time.withMinute(minute < 30 ? minute < 15 ? 15 : 30: 45);
}
Stream.iterate(time.truncatedTo(ChronoUnit.MINUTES), t -> t.plusMinutes(15L))
.limit(5)
.forEach(System.out::println);
The above code outputs the following:
11:00
11:15
11:30
11:45
12:00
Your job will just be to figure out the math to limit the Stream
size.
add a comment |
It would be much easier to work with the built-in Date/Time API, specifically LocalTime
:
LocalTime time = LocalTime.now();
int minute = time.getMinute();
if (minute > 45) {
time = time.plusHours(1L).withMinute(0);
} else {
time = time.withMinute(minute < 30 ? minute < 15 ? 15 : 30: 45);
}
Stream.iterate(time.truncatedTo(ChronoUnit.MINUTES), t -> t.plusMinutes(15L))
.limit(5)
.forEach(System.out::println);
The above code outputs the following:
11:00
11:15
11:30
11:45
12:00
Your job will just be to figure out the math to limit the Stream
size.
It would be much easier to work with the built-in Date/Time API, specifically LocalTime
:
LocalTime time = LocalTime.now();
int minute = time.getMinute();
if (minute > 45) {
time = time.plusHours(1L).withMinute(0);
} else {
time = time.withMinute(minute < 30 ? minute < 15 ? 15 : 30: 45);
}
Stream.iterate(time.truncatedTo(ChronoUnit.MINUTES), t -> t.plusMinutes(15L))
.limit(5)
.forEach(System.out::println);
The above code outputs the following:
11:00
11:15
11:30
11:45
12:00
Your job will just be to figure out the math to limit the Stream
size.
answered Nov 12 at 15:55
Jacob G.
15.2k52162
15.2k52162
add a comment |
add a comment |
Try this. Inserting items to array by times[i]
is incorrect in this scenario. It will only count the first loop items
public class Main
{
public static void main(String args)
{
String quarterHours = { "00", "15", "30", "45" };
String times = new String[96];
int count = 0;
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 4; j++)
{
String time = i + ":" + quarterHours[j];
if (i < 10)
{
time = "0" + time;
}
times[count] = "Today " + time;
count++;
}
}
for (int i = 0; i < times.length; i++)
{
System.out.println(times[i]);
}
}
}
add a comment |
Try this. Inserting items to array by times[i]
is incorrect in this scenario. It will only count the first loop items
public class Main
{
public static void main(String args)
{
String quarterHours = { "00", "15", "30", "45" };
String times = new String[96];
int count = 0;
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 4; j++)
{
String time = i + ":" + quarterHours[j];
if (i < 10)
{
time = "0" + time;
}
times[count] = "Today " + time;
count++;
}
}
for (int i = 0; i < times.length; i++)
{
System.out.println(times[i]);
}
}
}
add a comment |
Try this. Inserting items to array by times[i]
is incorrect in this scenario. It will only count the first loop items
public class Main
{
public static void main(String args)
{
String quarterHours = { "00", "15", "30", "45" };
String times = new String[96];
int count = 0;
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 4; j++)
{
String time = i + ":" + quarterHours[j];
if (i < 10)
{
time = "0" + time;
}
times[count] = "Today " + time;
count++;
}
}
for (int i = 0; i < times.length; i++)
{
System.out.println(times[i]);
}
}
}
Try this. Inserting items to array by times[i]
is incorrect in this scenario. It will only count the first loop items
public class Main
{
public static void main(String args)
{
String quarterHours = { "00", "15", "30", "45" };
String times = new String[96];
int count = 0;
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 4; j++)
{
String time = i + ":" + quarterHours[j];
if (i < 10)
{
time = "0" + time;
}
times[count] = "Today " + time;
count++;
}
}
for (int i = 0; i < times.length; i++)
{
System.out.println(times[i]);
}
}
}
answered Nov 12 at 15:53
Sand
1,020113
1,020113
add a comment |
add a comment |
Problem in the previous approach was that the last minutes-turn wins and overwrites any previous result.
I suggest using a List
, it is much easier to handle. You don't need to take care for the size.
String quarterHours = { "00", "15", "30", "45" };
List<String> times = new ArrayList<>();
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
times.add("Today " + time);
}
}
System.out.println(times);
Output:
[Today 00:00, Today 00:15, Today 00:30, Today 00:45, Today 01:00, Today 01:15, Today 01:30, Today 01:45, Today 02:00, Today 02:15, Today 02:30, Today 02:45, Today 03:00, Today 03:15, Today 03:30, Today 03:45, Today 04:00, Today 04:15, Today 04:30, Today 04:45, Today 05:00, Today 05:15, Today 05:30, Today 05:45, Today 06:00, Today 06:15, Today 06:30, Today 06:45, Today 07:00, Today 07:15, Today 07:30, Today 07:45, Today 08:00, Today 08:15, Today 08:30, Today 08:45, Today 09:00, Today 09:15, Today 09:30, Today 09:45, Today 10:00, Today 10:15, Today 10:30, Today 10:45, Today 11:00, Today 11:15, Today 11:30, Today 11:45, Today 12:00, Today 12:15, Today 12:30, Today 12:45, Today 13:00, Today 13:15, Today 13:30, Today 13:45, Today 14:00, Today 14:15, Today 14:30, Today 14:45, Today 15:00, Today 15:15, Today 15:30, Today 15:45, Today 16:00, Today 16:15, Today 16:30, Today 16:45, Today 17:00, Today 17:15, Today 17:30, Today 17:45, Today 18:00, Today 18:15, Today 18:30, Today 18:45, Today 19:00, Today 19:15, Today 19:30, Today 19:45, Today 20:00, Today 20:15, Today 20:30, Today 20:45, Today 21:00, Today 21:15, Today 21:30, Today 21:45, Today 22:00, Today 22:15, Today 22:30, Today 22:45, Today 23:00, Today 23:15, Today 23:30, Today 23:45]
add a comment |
Problem in the previous approach was that the last minutes-turn wins and overwrites any previous result.
I suggest using a List
, it is much easier to handle. You don't need to take care for the size.
String quarterHours = { "00", "15", "30", "45" };
List<String> times = new ArrayList<>();
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
times.add("Today " + time);
}
}
System.out.println(times);
Output:
[Today 00:00, Today 00:15, Today 00:30, Today 00:45, Today 01:00, Today 01:15, Today 01:30, Today 01:45, Today 02:00, Today 02:15, Today 02:30, Today 02:45, Today 03:00, Today 03:15, Today 03:30, Today 03:45, Today 04:00, Today 04:15, Today 04:30, Today 04:45, Today 05:00, Today 05:15, Today 05:30, Today 05:45, Today 06:00, Today 06:15, Today 06:30, Today 06:45, Today 07:00, Today 07:15, Today 07:30, Today 07:45, Today 08:00, Today 08:15, Today 08:30, Today 08:45, Today 09:00, Today 09:15, Today 09:30, Today 09:45, Today 10:00, Today 10:15, Today 10:30, Today 10:45, Today 11:00, Today 11:15, Today 11:30, Today 11:45, Today 12:00, Today 12:15, Today 12:30, Today 12:45, Today 13:00, Today 13:15, Today 13:30, Today 13:45, Today 14:00, Today 14:15, Today 14:30, Today 14:45, Today 15:00, Today 15:15, Today 15:30, Today 15:45, Today 16:00, Today 16:15, Today 16:30, Today 16:45, Today 17:00, Today 17:15, Today 17:30, Today 17:45, Today 18:00, Today 18:15, Today 18:30, Today 18:45, Today 19:00, Today 19:15, Today 19:30, Today 19:45, Today 20:00, Today 20:15, Today 20:30, Today 20:45, Today 21:00, Today 21:15, Today 21:30, Today 21:45, Today 22:00, Today 22:15, Today 22:30, Today 22:45, Today 23:00, Today 23:15, Today 23:30, Today 23:45]
add a comment |
Problem in the previous approach was that the last minutes-turn wins and overwrites any previous result.
I suggest using a List
, it is much easier to handle. You don't need to take care for the size.
String quarterHours = { "00", "15", "30", "45" };
List<String> times = new ArrayList<>();
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
times.add("Today " + time);
}
}
System.out.println(times);
Output:
[Today 00:00, Today 00:15, Today 00:30, Today 00:45, Today 01:00, Today 01:15, Today 01:30, Today 01:45, Today 02:00, Today 02:15, Today 02:30, Today 02:45, Today 03:00, Today 03:15, Today 03:30, Today 03:45, Today 04:00, Today 04:15, Today 04:30, Today 04:45, Today 05:00, Today 05:15, Today 05:30, Today 05:45, Today 06:00, Today 06:15, Today 06:30, Today 06:45, Today 07:00, Today 07:15, Today 07:30, Today 07:45, Today 08:00, Today 08:15, Today 08:30, Today 08:45, Today 09:00, Today 09:15, Today 09:30, Today 09:45, Today 10:00, Today 10:15, Today 10:30, Today 10:45, Today 11:00, Today 11:15, Today 11:30, Today 11:45, Today 12:00, Today 12:15, Today 12:30, Today 12:45, Today 13:00, Today 13:15, Today 13:30, Today 13:45, Today 14:00, Today 14:15, Today 14:30, Today 14:45, Today 15:00, Today 15:15, Today 15:30, Today 15:45, Today 16:00, Today 16:15, Today 16:30, Today 16:45, Today 17:00, Today 17:15, Today 17:30, Today 17:45, Today 18:00, Today 18:15, Today 18:30, Today 18:45, Today 19:00, Today 19:15, Today 19:30, Today 19:45, Today 20:00, Today 20:15, Today 20:30, Today 20:45, Today 21:00, Today 21:15, Today 21:30, Today 21:45, Today 22:00, Today 22:15, Today 22:30, Today 22:45, Today 23:00, Today 23:15, Today 23:30, Today 23:45]
Problem in the previous approach was that the last minutes-turn wins and overwrites any previous result.
I suggest using a List
, it is much easier to handle. You don't need to take care for the size.
String quarterHours = { "00", "15", "30", "45" };
List<String> times = new ArrayList<>();
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
times.add("Today " + time);
}
}
System.out.println(times);
Output:
[Today 00:00, Today 00:15, Today 00:30, Today 00:45, Today 01:00, Today 01:15, Today 01:30, Today 01:45, Today 02:00, Today 02:15, Today 02:30, Today 02:45, Today 03:00, Today 03:15, Today 03:30, Today 03:45, Today 04:00, Today 04:15, Today 04:30, Today 04:45, Today 05:00, Today 05:15, Today 05:30, Today 05:45, Today 06:00, Today 06:15, Today 06:30, Today 06:45, Today 07:00, Today 07:15, Today 07:30, Today 07:45, Today 08:00, Today 08:15, Today 08:30, Today 08:45, Today 09:00, Today 09:15, Today 09:30, Today 09:45, Today 10:00, Today 10:15, Today 10:30, Today 10:45, Today 11:00, Today 11:15, Today 11:30, Today 11:45, Today 12:00, Today 12:15, Today 12:30, Today 12:45, Today 13:00, Today 13:15, Today 13:30, Today 13:45, Today 14:00, Today 14:15, Today 14:30, Today 14:45, Today 15:00, Today 15:15, Today 15:30, Today 15:45, Today 16:00, Today 16:15, Today 16:30, Today 16:45, Today 17:00, Today 17:15, Today 17:30, Today 17:45, Today 18:00, Today 18:15, Today 18:30, Today 18:45, Today 19:00, Today 19:15, Today 19:30, Today 19:45, Today 20:00, Today 20:15, Today 20:30, Today 20:45, Today 21:00, Today 21:15, Today 21:30, Today 21:45, Today 22:00, Today 22:15, Today 22:30, Today 22:45, Today 23:00, Today 23:15, Today 23:30, Today 23:45]
answered Nov 12 at 15:55
michaeak
749315
749315
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53265558%2fjava-create-an-array-of-times-15-minute-intervals-between-current-time-and-a%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
You're overwriting the time each cycle of the inner loop. You should use a
List<String>
instead and just append without worrying about indexes.– Federico klez Culloca
Nov 12 at 15:48
1
Thanks @K.Nicholas for the equation to round up to the nearest quarter hour. I will use this and combine with the marked answer to get my desired result. I didn't pay attention in maths class either, you're right.
– JamLis
Nov 12 at 16:12