i want to make countdown timer for android by getting the end date string from firebase database. when i try the end date string as simple variable it works but when i get exact the same date string from firebase database it doesnt work or i dont know what should i do. heres my code
public class DashboardFragment extends Fragment {
private View dashboardFragment;
private DatabaseReference timeRef;
public String END_TIME="";
private TextView days, hour, minute,second;
private CountDownTimer mCountDownTimer;
private long mTimeLeftInMillis;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
dashboardFragment= inflater.inflate(R.layout.fragment_dashboard, container, false);
linear_layout_1 = dashboardFragment.findViewById(R.id.linear_layout_1);
linear_layout_2 = dashboardFragment.findViewById(R.id.linear_layout_2);
days = dashboardFragment.findViewById(R.id.days);
hour = dashboardFragment.findViewById(R.id.hour);
minute = dashboardFragment.findViewById(R.id.minute);
second = dashboardFragment.findViewById(R.id.second);
timeRef=FirebaseDatabase.getInstance().getReference().child("Time");
mTimeLeftInMillis=dateFormat();
ValueEventListener timeEvent=new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
END_TIME =snapshot.child("endTime").getValue(String.class);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
timeRef.addListenerForSingleValueEvent(timeEvent);
startTimer();
return dashboardFragment;
}
private long dateFormat() {
long diff=0;
SimpleDateFormat formatter=new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);
String endTime_=END_TIME;
long endTime=0;
Date endDate;
try {
endDate=formatter.parse(endTime_);
endTime= Objects.requireNonNull(endDate).getTime();
}catch ( ParseException e){
e.printStackTrace();
}
long startTime = System.currentTimeMillis();
diff=endTime-startTime;
return diff;
}
private void startTimer() {
mCountDownTimer= new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
long Days = mTimeLeftInMillis / (24 * 60 * 60 * 1000);
long Hours = mTimeLeftInMillis / (60 * 60 * 1000) % 24;
long Minutes = mTimeLeftInMillis / (60 * 1000) % 60;
long Seconds = mTimeLeftInMillis / 1000 % 60;
//
days.setText(String.format("%02d", Days));
hour.setText(String.format("%02d", Hours));
minute.setText(String.format("%02d", Minutes));
second.setText(String.format("%02d", Seconds));
}
@Override
public void onFinish() {
}
};
mCountDownTimer.start();
}
as i said earlier when i set like,
END_TIME="06.05.2021 05:56"
the only problem is it doestnt work when i take the same string from firebase database. any one knw the solution i appreciate alot.