Search This Blog

Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Friday, July 13, 2018

Android Shared preferences example

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.apply();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

Tuesday, March 27, 2018

Lấy thời gian trong Java – Time in Java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class TimeInJava{
    public static void main(String[] args){
        long start, end;
         
        start = System.nanoTime();  // start lấy thời gian theo nanosecond
        for (int i=0; i<100; i++);
        end = System.nanoTime();    // start lấy thời gian theo nanosecond
        System.out.println("Time Nano: " + (end - start));
         
        start = System.currentTimeMillis(); // start lấy thời gian theo millisecond
        for (long i=0; i<100000000; i++);    //vòng lặp không thực hiện thêm lệnh nào
        end = System.currentTimeMillis();   // start lấy thời gian theo millisecond
        System.out.println("Time Millis: " + (end - start));
         
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // tạo 1 đối tượng có định dạng thời gian yyyy-MM-dd HH:mm:ss
        Date date = new Date(); // lấy thời gian hệ thống
        String stringDate = dateFormat.format(date);//Định dạng thời gian theo trên
        System.out.println("Date: " + stringDate);
    }
}