0

I have a basic app that has RecyclerView for getting data from a Firebase database, which looks like this:

enter image description here

I would like to have the 20170108_100 converted into data and time format (08/01/2017 10:00), which can then be compared to the actual date.

I'm planning to have some IF conditions in order to sort the database by date&time and I need to have something like this:

if (20170108_100 < CurrentDate&Time) then doSomething else doSomethingElse

Is there any way one can do that?


So far, I have this code that reads that database:

public class Hotarari extends Fragment {

  View v;
  ProgressDialog progress;
  private FirebaseRecyclerAdapter < Variabile_primarie, ContactViewHolder > cc;
  private RecyclerView mContactRV;
  private DatabaseReference mPostRef;


  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_hotarari, container, false);
    return v;
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    this.v = view;
    initialiseScreen();
  }

  private void initialiseScreen() {
    mContactRV = (RecyclerView) v.findViewById(R.id.contact_recyclerview);
    mContactRV.setLayoutManager(new LinearLayoutManager(getContext()));
    mPostRef = FirebaseDatabase.getInstance().getReference("-DeciziiPrimari");
    setupAdaptater();
    mContactRV.setAdapter(cc);
  }

  private void setupAdaptater() {
    cc = new FirebaseRecyclerAdapter < Variabile_primarie, ContactViewHolder > (
      Variabile_primarie.class,
      R.layout.item_hotarari,
      ContactViewHolder.class,
      mPostRef
    ) {
      @Override
      protected void populateViewHolder(ContactViewHolder viewHolder, final Variabile_primarie model, int position) {
        viewHolder.setData(model.getData());
        viewHolder.setNumar(model.getNumar());
        viewHolder.setHotarare(model.getHotarare());
      }
    };
  }

  public static class ContactViewHolder extends RecyclerView.ViewHolder {
    private TextView tv_hotarare;
    private TextView tv_data;
    private TextView tv_numar;

    public ContactViewHolder(View itemView) {
      super(itemView);
      tv_hotarare = (TextView) itemView.findViewById(R.id.continut_hotarare);
      tv_data = (TextView) itemView.findViewById(R.id.data_hotarare);
      tv_numar = (TextView) itemView.findViewById(R.id.numar_hotarare);
    }
    void setHotarare(String hotarare) {
      tv_hotarare.setText(String.valueOf(hotarare));
    }

    public void setData(String data) {
      tv_data.setText(String.valueOf(data));
    }

    void setNumar(String numar) {
      tv_numar.setText(String.valueOf(numar));
    }
  }
}
CuriousPaul
  • 449
  • 8
  • 20
  • Any reason why you aren't storing the date as a simple long? That makes everything much easier, because then you can query date ranges with numbers, and use other libraries that understand dates as numbers. – Doug Stevenson Mar 01 '18 at 20:22
  • There is no reason for not having it long. I can change it to long. But that `data` string is independent on the 2017xxx childName. I add data in Firebase manually. – CuriousPaul Mar 01 '18 at 20:28
  • So, if you use a long for the date, you no longer need to look at the key and try to convert it into a date. Just use the number everywhere. – Doug Stevenson Mar 01 '18 at 20:30
  • But I need to compare it with the current date for the `if` condition, therefore I need to convert it from long/string to date. Or? – CuriousPaul Mar 01 '18 at 20:36
  • System.currentTimeMillis() gives you the current date in Java. – Doug Stevenson Mar 01 '18 at 20:37
  • And do I just write `if (date < System.currentTimeMillis()) then doSomething else doSomethingElse`? – CuriousPaul Mar 01 '18 at 20:38
  • Try it for yourself. – Doug Stevenson Mar 01 '18 at 20:39
  • If you're looking to parse your current format, this seems closest: https://stackoverflow.com/questions/10013998/fastest-way-to-parse-a-yyyymmdd-date-in-java – Frank van Puffelen Mar 01 '18 at 21:50

0 Answers0