Replicate column Header - java

I Guys, when I enter more than one store, the header columns are duplicated(if the store are two, if store are three the header colums triplicate). Example: if I choose two stores you create two headers (the duplicate header does not have rows). I leave you the code in case you can help me I would be very grateful.
deliveringGrid.getChildren().clear();
Date startDate = dateFrom.getValue();
Date endDate = dateTo.getValue();
List<Date> dates = new ArrayList<Date>();
long start = startDate.getTime();
long end = endDate.getTime();
long interval = 24 * 1000 * 60 * 60;
while (start <= end)
{
dates.add(new Date(start));
start += interval;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
columns = new Columns();
columns.setStyle("text-align: center;");
rows = new Rows();
Column col = new Column();
col.setStyle("width: 120px; background: rgb(195,218,249);");
Label label = new Label(getLabel(STORE).toUpperCase());
label.setStyle("color: rgb(21,66,151); font-weight: bold;");
col.appendChild(label);
columns.appendChild(col);
for (StoreManagerModel storeManagerModel : storeList)
{
Row storeRow = new Row();
storeRow.setStyle("text-align: center;");
Label storeName = new Label(storeManagerModel.getName() + " - " + storeManagerModel.getStoreRef());
storeName.setStyle("color: rgb(21,66,151); font-weight: bold;");
storeRow.appendChild(storeName);
for (Date date : dates)
{
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
List<List<Object>> query = orderDeliveringService.getOrderDeliveringWithDayDistribution(date, storeManagerModel);
List<OrderDeliveringModel> orderDeliveringModelList = OrderDeliveringConverter.convertData(query);
Column dayCol = new Column();
dayCol.setStyle(COLUMN_HEADERS_STYLE);
int dayOfWeek = TimeSlotDateUtils.getDayOfWeek(date);
String dayString = getDayOfWeekAsString(dayOfWeek);
Label headerLabel = new Label(dayString + " " + dateFormat.format(date));
dayCol.appendChild(headerLabel);
Label day1;
if (CollectionUtils.isNotEmpty(orderDeliveringModelList))
{
StoreManagerModel orderDeliveringModelStore = orderDeliveringModelList.get(0).getStore();
Date orderDeliveringModelData = orderDeliveringModelList.get(0).getDay();
Integer orderDeliveringModelOrder = orderDeliveringModelList.get(0).getOrderCount().get(0);
Integer orderDeliveringModelTotalCapacity = orderDeliveringModelList.get(0).getTotalCapacity().get(0);
day1 = new Label(orderDeliveringModelOrder + " / " + orderDeliveringModelTotalCapacity);
day1.setAttribute("storePlusDate", orderDeliveringModelStore.getStoreRef() + "*" + dateFormat.format(date));
}
else
{
day1 = new Label(" - / - ");
day1.setAttribute("storePlusDate", storeManagerModel.getStoreRef() + "*" + dateFormat.format(date));
}
columns.appendChild(dayCol);
storeRow.appendChild(day1);
}
rows.appendChild(storeRow);
}
deliveringGrid.appendChild(columns);
deliveringGrid.appendChild(rows);

Related

how to use setSelection on dateRangePicker of material-component?

I want to display a selected dates range when open the dateRangePicker, I knew the method setSelected is for this and I tried it its not work, or maybe am doing something wrong, please help
here what I tried already
public void openDateRangePicker(View view) {
MaterialDatePicker.Builder builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
builder.setCalendarConstraints(constraintsBuilder.build());
picker.setStyle(DialogFragment.STYLE_NORMAL, R.style.Custom_MaterialCalendar_Fullscreen);
if(!firstDateStr.isEmpty() || !endDateStr.isEmpty()){
builder.setSelection(selectionDates);
}
picker.show(getSupportFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
#Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
long firstDateLong = selection.first;
Date firstDate=new Date(firstDateLong);
long endDateLong = selection.second;
Date endDate=new Date(endDateLong);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
//format yyyy-MM-dd
firstDateStr = sdf2.format(firstDate);
endDateStr = sdf2.format(endDate);
selectionDates = selection;
selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
tvDates.setText(selectedDatesStr);
tvDates.setTypeface(Typeface.DEFAULT_BOLD);
picker.dismiss();
}
});
}
what I did here is save the selection object and use it to show the range when open the picker next time, but it opens without any selection like it open for the first time!
order of your lines matters,you need to call builder.build(); after finishing setting up the builder.
String firstDateStr="";
String endDateStr="";
Pair<Long, Long> selectionDates=null;
public void openDateRangePicker() {
MaterialDatePicker.Builder builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());
// picker.setStyle(DialogFragment.STYLE_NORMAL);
if(selectionDates!=null){
builder.setSelection(selectionDates);
}
MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
#Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
long firstDateLong = selection.first;
Date firstDate=new Date(firstDateLong);
long endDateLong = selection.second;
Date endDate=new Date(endDateLong);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
//format yyyy-MM-dd
firstDateStr = sdf2.format(firstDate);
endDateStr = sdf2.format(endDate);
selectionDates = selection;
selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
tvDates.setText(selectedDatesStr);
tvDates.setTypeface(Typeface.DEFAULT_BOLD);
picker.dismiss();
}
});
}

How to get Time Slot based on 1hour interval

I want to store time slot in the arraylist. i have start time and end time. based on start time it should create time slot.
For example if start time is 09:00AM and end time is 21:00PM then it should add into arraylist like below
09:00AM
10:00AM
11:00AM
12:00PM
13:00PM
14:00PM
..... so on
21:00PM
so one user books 13:00PM to 15:00PM slots so it should not be available to another user and other slot should be available. how to compare already booking time with new array list.
Code
private void getStartHourArray() {
times = new ArrayList<TimeSlot>();
Calendar calender = Calendar.getInstance();
calender.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
int ti = calender.get(Calendar.HOUR_OF_DAY);
int minutes = calender.get(Calendar.MINUTE);
System.out.println(minutes);
String[] quarterHours = {
"00",
"30",
};
boolean isflag = false;
times = new ArrayList<>();
for (int i = 9; i < 22; i++) {
if (ti > 8) {
for (int j = 0; j < 2; j++) {
if ((i == ti && minutes < Integer.parseInt(quarterHours[j])) || (i != ti) || isflag == true) {
isflag = true;
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
String hourFormat = i + ":" + quarterHours[j];
if (i < 12) {
hourFormat = time + " AM";
} else
hourFormat = time + " PM";
TimeSlot t = new TimeSlot();
t.time = hourFormat;
t.isAvailable = "Available";
times.add(t);
}
}
}
}
if (times != null) {
load.setVisibility(View.GONE);
}
}
Time Slot model class
public class TimeSlot {
public String time;
public String isAvailable;
}
Try something like this :
String firstDate = "26/02/2019";
String firstTime = "00:00 AM";
String secondDate = "26/02/2019";
String secondTime = "12:00 PM";
String format = "dd/MM/yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = sdf.parse(firstDate + " " + firstTime);
Date dateObj2 = sdf.parse(secondDate + " " + secondTime);
System.out.println("Date Start: "+dateObj1);
System.out.println("Date End: "+dateObj2);
long dif = dateObj1.getTime();
while (dif < dateObj2.getTime()) {
Date slot = new Date(dif);
System.out.println("Hour Slot --->" + slot);
dif += 3600000;
}
This will give you a time slot for each hour, add this in ArrayList and when any user select time then remove that from ArrayList and update to the server so when next
user tries to get data it won't get the first selected user time slot.
try this:
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
public class PlayGround {
private Map<LocalTime, Boolean> slots = new HashMap();
public static void main(String[] args) {
PlayGround client = new PlayGround();
client.initializeSlots();
client.allocateSlots("10:00", "13:00");
//this shouldn't be available
client.allocateSlots("11:00", "12:00");
//not sure if u want this to be available. since it is start when the 1st just finished.
client.allocateSlots("13:00", "15:00");
client.allocateSlots("16:00", "18:00");
}
private void initializeSlots() {
LocalTime time = LocalTime.of(9, 0);
slots.put(time, true);
for (int i = 1; i < 24; i++) {
slots.put(time.plusHours(i), true);
}
}
private void allocateSlots(String strTime, String edTime) {
LocalTime startTime = LocalTime.parse(strTime);
LocalTime endTime = LocalTime.parse(edTime);
while (startTime.isBefore(endTime)) {
//check if the time slots between start and end time are available
if (!slots.get(startTime) || !slots.get(endTime)) {
System.out.println("slots not available" + " start time: " + strTime + " end time: " + edTime);
return;
}
startTime = startTime.plusHours(1);
endTime = endTime.minusHours(1);
}
System.out.println("slots are available" + " start time: " + strTime + " end time: " + edTime);
//then here u can mark all slots between to unavailable.
startTime = LocalTime.parse(strTime);
endTime = LocalTime.parse(edTime);
while (startTime.isBefore(endTime)) {
slots.put(startTime, false);
slots.put(endTime, false);
startTime = startTime.plusHours(1);
endTime = endTime.minusHours(1);
}
}
}

How to set text by date and increment loop android?

I want to set text by date and incrementing loop, and when the day changes looping start from the beginning.
Example
1. day 1 =
a. nameFile 110920190001
b. nameFile 110920190002, etc.
2. day 2 =
a. nameFile 120920190001
b. nameFile 120920190002, etc.
Code
Date documentsDate = Calendar.getInstance().getTime();
SimpleDateFormat documentDates = new SimpleDateFormat("ddMMyy");
String setTitleDocument = documentDates.format(documentsDate);
for(int i = 1; i <= 1000; i++) {
String countDocument = String.format("%04d", i);
textNameDocument.setText("Document " + setTitleDocument + countDocument);
}
Just put the Date Initialization in the for loop for it to always take the new Instance of the date.
public static void replace(String s) {
for (int i = 1; i <= 1000; i++) {
Date documentsDate = Calendar.getInstance().getTime();
SimpleDateFormat documentDates = new SimpleDateFormat("ddMMyy");
String setTitleDocument = documentDates.format(documentsDate);
String countDocument = String.format("%04d", i);
textNameDocument.setText("Document " + setTitleDocument + countDocument);
}
}

Get available days between two days in java

I am newly for java, and i want to build booking system and now i try to get available days between two days without booked days. but, however my code running bad.
this is my code
Connection connection = DBConnection.getInstance().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from A");
ResultSet executeQuery = preparedStatement.executeQuery();
while (executeQuery.next()) {
String refid = executeQuery.getString("REFID");
statDate = executeQuery.getDate("Start_date");
endDate = executeQuery.getDate("End_date");
System.out.println("ref no : " + refid + " statDate :" + statDate + " end date :" + endDate);
System.out.println("\n");
int month = statDate.getMonth();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//
for (int i = 1; i < maxDay; i++) {
cal.set(Calendar.DAY_OF_MONTH, i + 1);
String format = df.format(cal.getTime());
dateOfMonth.add(format);
addDays = addDays(statDate, 2);
if (df.format(cal.getTime()).equals("" + statDate) || df.format(cal.getTime()).equals("" + endDate)) {
//System.out.println(statDate + " is not available !");
System.out.println("---");
} else {
int bDays = endDate.getDate() - statDate.getDate();
//System.out.println(bDays);
//===========
List<DateTime> between = getDateRange(DateTime.parse(statDate.toString()), DateTime.parse(endDate.toString()));
for (DateTime dateTime : between) {
Date da = dateTime.toDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateOfBooked.add(sdf.format(da));
}
System.out.println(df.format(cal.getTime()));
}
}
System.out.println("\n");
}
this is my db script
insert into A values('1','ref1','2018-02-18','2018-02-25');
insert into A values('2','ref1','2018-03-10','2018-03-17');
insert into A values('3','ref1','2018-03-20','2018-03-27');
i try to get available days from thees booked days. how can i print only available days ?
output

How to get the list of dates in a given date range?

I am trying to get the list of dates between in a date range in iso format ("yyyy-MM-dd"), as follows:
ArrayList<String> datesList = new ArrayList<String>();
try {
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");
int[] fromDateParts = new int[3];
for (int f = 0; f < fromIsoDate.split("-").length; f++) {
fromDateParts[f] = Integer.parseInt(fromIsoDate.split("-")[f]);
System.out.println("fromDateParts[" + f + "] : " + fromDateParts[f]);
}
int[] toDateParts = new int[3];
for (int t = 0; t < toIsoDate.split("-").length; t++) {
toDateParts[t] = Integer.parseInt(toIsoDate.split("-")[t]);
System.out.println("toDateParts[" + t + "] : " + toDateParts[t]);
}
Calendar fromDate = new GregorianCalendar(fromDateParts[0], fromDateParts[1], fromDateParts[2]);
Calendar toDate = new GregorianCalendar(toDateParts[0], toDateParts[1], toDateParts[2]);
System.out.println("fromDate " + fromDate + " toDate " + toDate);
boolean hasMoreDays = true;
while (hasMoreDays) {
if ((fromDate.before(toDate)) || (fromDate.equals(toDate))) {
datesList.add(isoFormat.format(fromDate));
fromDate.add(Calendar.DAY_OF_MONTH, 1);
} else {
hasMoreDays = false;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception : While determining the dates in between " + fromIsoDate + " and " + toIsoDate + "!");
}
But, it throws error:
java.lang.IllegalArgumentException: Cannot format given Object as a Date.
Please guide me in getting the list of dates between in a date range!
Just wrap your start date in a Calendar and use that:
Date startDate;
Date endDate;
Calendar c = Calendar.getInstance();
c.setTime(startDate);
Date dt = startDate;
List<Date> dates = new ArrayList<>();
do {
dates.add(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();
} while (endDate.getTime() > dt.getTime());
As you would expect to hear, the Java 8 API or possibly JodaTime might make your life easier than using the Java 7 date way of things.
You should pass a Date to your SimpleDateFormat.fotmat inntead of Calendar:
datesList.add(isoFormat.format(fromDate));
to
datesList.add(isoFormat.format(fromDate.getTime));
And below is my solution for your request:
String fromStr = "2016-11-01";
String toStr = "2016-11-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar from = Calendar.getInstance();
from.setTime(sdf.parse(fromStr));
Calendar to = Calendar.getInstance();
to.setTime(sdf.parse(toStr));
List<Date> retval = new ArrayList<Date>();
while (from.before(to) || from.equals(to)) {
retval.add(from.getTime());
System.out.println(sdf.format(from.getTime()));
from.add(Calendar.DATE, 1);
}
Hope this help!
If you use java8 you can write the whole stuff in one line as shown below, I have used java.util.Date
List<Date> dates = new ArrayList<>();
Date fromDate = new Date(2016, 11, 02);
Date toDate = new Date(2016, 11, 28);
dates.add(new Date(2016, 11, 01));
dates.add(new Date(2016, 11, 02));
dates.add(new Date(2016, 11, 03));
dates.add(new Date(2016, 11, 04));
List<Date> filteredDates = dates.stream()
.filter((e) -> e.after(fromDate) && e.before(toDate)).collect(Collectors.toList());

Categories

Resources