Home > Blockchain >  How to save current time or date
How to save current time or date

Time:11-16

How can I save the current time and date,I just want the date and time of the last time that I entered that activity

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    CardView cardView = view.findViewById(R.id.card_view2);
     textView = view.findViewById(R.id.dateTv);
     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
            mydate = getPrefs.getString("Test","");
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            
            Calendar calendar= Calendar.getInstance();
            Date time = Calendar.getInstance().getTime();
            
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy 'at' HH:mm:ss", Locale.getDefault());
            mydate = simpleDateFormat.format(calendar.getTime());
            textView.setText(mydate);
        }
    });
}

@Override
public void onPause() {
    super.onPause();
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("Test", mydate);
    editor.apply();
}

when i restart the app, it doesnt work

CodePudding user response:

Use this code.

Latter you have to convert milliseconds to date and time.

@Override
public void onPause() {
    super.onPause();
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("Test", System.currentTimeMillis() "");
    editor.apply();
}

Here is the code to convert milliseconds to date and time.

public static String getDateTime(String milli) {
        long milliSeconds = Long.parseLong(milli);
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy',' hh:mm a", Locale.ENGLISH);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);

        return formatter.format(calendar.getTime());
    }

CodePudding user response:

Lifecycle

You commented:

I didn't get an error message. but the date/time its working fine,when i restart the app ,the time empty again

I don’t know Android, so these are just wild guesses…

(A) I do not see where you display the moment upon launch of the app. Looks to me like you retrieve the stored value, but do not display it. Shouldn’t your onViewCreated call textView.setText(mydate);?

(B) Write the moment to storage immediately upon its capture. That is, within your onClick handler.

I looked briefly at the method doc, and the lifecycle doc. It is not clear to me whether or not the user can exit your activity without necessarily firing the onPause event.

Avoid legacy classes

Your question’s code uses terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

Never use Date, Calendar, SimpleDateFormat, etc.

Instant

To capture the current moment as seen in UTC (an offset of zero hours-minutes-seconds), use Instant.

Instant instant = Instant.now() ;

For storage, logging, and data-exchange, generate text in standard ISO 8601 format.

String output = instant.now() ;

2022-01-23T15:30:45.786347Z

  • Related