Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] daterangepicker – Dates are not printing as date format in android

Posted on November 14, 2022

Solution 1 :

You simply have to use a DateFormatter class. There are default formats. You just instantiate it, then call format on it, passing the Date.

Here is the documentation: DateFormatter.

Dates in Android are a special drag because war broke out between Oracle and Google about the Java licensing terms. So Java was not updated for a while, including Java 8 which included a complete rewrite of dates and times. You can now use Java 8 Dates/Times in Android but if you do you lose compatability with older OSes.

Here is your code, with the formatter added:

   public void onPositiveButtonClick(Pair<Long, Long> selection) {
       tv1.setText(materialDatePicker.getHeaderText());
        startDate = selection.first;
        endDate = selection.second;
        DateFormatter formatter = SimpleDateFormatter("dd-MM-yyyy", Locale.US);
        System.out.println("this is start date: " + formatter.format(startDate));
        System.out.println("this is end date " + formatter.format(endDate)));

    }

Of course you could use the default Locale.

Problem :

what I should do if i want to print these dates? Because using System.out.println(startDate); System.out.println(endDate); gives me this output : 1595203200000 1596153600000 and I want date as output

public class MainActivity extends AppCompatActivity {

private Button bt1;
private TextView tv1;
private Long startDate;
private Long endDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    bt1 = findViewById(R.id.bt1);
    tv1 = findViewById(R.id.tv1);

    MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
    final MaterialDatePicker<Pair<Long,Long>> materialDatePicker = builder.build();

    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            materialDatePicker.show(getSupportFragmentManager(),"date picker");
        }
    });

    /*materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
        @Override
        public void onPositiveButtonClick(Object selection) {
           tv1.setText(materialDatePicker.getHeaderText());
        }
    });*/
    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onPositiveButtonClick(Pair<Long, Long> selection) {
           tv1.setText(materialDatePicker.getHeaderText());
            startDate = selection.first;
            endDate = selection.second;
            System.out.println("this is start date: " + startDate);
            System.out.println("this is end date " + endDate);

        }
    });

}

}

the current output is : I/System.out: this is start date: 1594598400000 this is end date 1596672000000

Comments

Comment posted by priks

Can you please share the code line for this as I am facing some problem while writting these.

READ  [ANSWERED] android - Data Snapshot returning null value
Powered by Inline Related Posts

Comment posted by Rob

@priks example is there

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress