Here is a sample code in Android Java to create two different time lists, one for past times and one for coming times, based on the current time and the provided start and end times:

 java

package com.example.camera2;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.camera2.databinding.ActivityTimetestBinding;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class timetest extends AppCompatActivity {
TimeZone timeZone = TimeZone.getTimeZone("Asia/Karachi");
Calendar calendar = Calendar.getInstance(timeZone);
Date currentTime = calendar.getTime();
ActivityTimetestBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= ActivityTimetestBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

// final DateFormat TIME_FORMAT = new SimpleDateFormat("hh:mm", Locale.US);
// Get the current time
// Date currentTime = Calendar.getInstance().getTime();

// Initialize the lists for past and coming times
List<Date> pastTimes = new ArrayList<>();
List<Date> comingTimes = new ArrayList<>();

// Loop through the provided time slots
List<TimeSlot> timeSlots = getTimeSlots();
for (TimeSlot timeSlot : timeSlots) {
Date sessionTime = null;
// Convert session date and time to Pakistan time zone
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM, EEEE, hh:mm a");
dateFormat.setTimeZone(timeZone);
String currentTimeFormatted = dateFormat.format(currentTime);
String TimeFormatted;
Toast.makeText(this, ""+Integer.parseInt(timeSlot.starttime.split(":")[0]), Toast.LENGTH_SHORT).show();
if(Integer.parseInt(timeSlot.starttime.split(":")[0])>=8&&Integer.parseInt(timeSlot.starttime.split(":")[0])<12){
TimeFormatted = currentTimeFormatted.split("-")[0]+"-"+currentTimeFormatted.split("-")[1].split(",")[0]+", "+timeSlot.getday() + ", " + timeSlot.getStarttime() + " am";
Toast.makeText(this, "ii", Toast.LENGTH_SHORT).show();
}else{
TimeFormatted = currentTimeFormatted.split("-")[0]+"-"+currentTimeFormatted.split("-")[1].split(",")[0]+", "+timeSlot.getday() + ", " + timeSlot.getStarttime() + " pm";
}

Date currentTimeDate = null;
Date TimeFormattedDate = null;
try {
currentTimeDate = dateFormat.parse(currentTimeFormatted);
TimeFormattedDate = dateFormat.parse(TimeFormatted);

} catch (ParseException e) {
e.printStackTrace();
}
try {
sessionTime = dateFormat.parse(timeSlot.getday() + ", " + currentTimeFormatted);
// sessionTime = dateFormat.parse(timeSlot.getday() + ", " + timeSlot.getStarttime() + ":" + timeSlot.getEndtime());
} catch (ParseException e) {
e.printStackTrace();
}


if (TimeFormattedDate.before(currentTimeDate)) {
pastTimes.add(TimeFormattedDate);
} else {
comingTimes.add(TimeFormattedDate);
}
binding.txtView1.setText(currentTimeFormatted+"="+TimeFormatted+"\ncoming="+new Gson().toJson(comingTimes) +"\nPast="+new Gson().toJson(pastTimes) );
}
Toast.makeText(this, ""+comingTimes, Toast.LENGTH_SHORT).show();
// Print the results
System.out.println("Past times: " + pastTimes);
System.out.println("Coming times: " + comingTimes);

///LocalTime format 8:00
// Get the current day of the week and time
// DayOfWeek currentDay = LocalDate.now().getDayOfWeek();
// LocalTime currentTime = LocalTime.now();
//
//// Create lists for "coming" and "past" time slots
// List<TimeSlot> comingTimeSlots = new ArrayList<>();
// List<TimeSlot> pastTimeSlots = new ArrayList<>();
//
//// Iterate over all time slots
// for (TimeSlot timeSlot : timeSlots) {
// // Parse the start time of the time slot
// LocalTime startTime = LocalTime.parse(timeSlot.getStarttime());
//
// // Check if the time slot is for the current day and the start time is after the current time
// if (timeSlot.getday().equals(currentDay.toString()) && startTime.isAfter(currentTime)) {
// // The time slot is "coming"
// comingTimeSlots.add(timeSlot);
// } else {
// // The time slot is "past"
// pastTimeSlots.add(timeSlot);
// }
// }
// binding.txtView1.setText("=\ncoming="+new Gson().toJson(comingTimeSlots) +"\nPast="+new Gson().toJson(pastTimeSlots) );


///

}

static class TimeSlot {
private String starttime;
private String endtime;
private String day;

public TimeSlot(String starttime, String endtime,String day) {
this.starttime = starttime;
this.endtime = endtime;
this.day = day;
}

public String getStarttime() {
return starttime;
}

public String getEndtime() {
return endtime;
}
public String getday() {
return day;
}
}
private static List<TimeSlot> getTimeSlots() {
List<TimeSlot> timeSlots = new ArrayList<>();
timeSlots.add(new TimeSlot("08:30", "10:00","Monday"));
timeSlots.add(new TimeSlot("08:30", "10:00", "Tuesday"));
timeSlots.add(new TimeSlot("11:30", "01:00", "Wednesday"));
timeSlots.add(new TimeSlot("02:30", "04:00", "Thursday"));
timeSlots.add(new TimeSlot("07:00", "5:30", "Friday"));
timeSlots.add(new TimeSlot("1:00", "5:30", "Friday"));
timeSlots.add(new TimeSlot("09:30", "10:00", "Saturday"));
timeSlots.add(new TimeSlot("10:30", "04:00", "Thursday"));
timeSlots.add(new TimeSlot("09:30", "04:00", "Thursday"));


return timeSlots;
}
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.