Java tutorial
//package com.java2s; import java.util.Calendar; import android.content.Context; import android.content.SharedPreferences; public class Main { public final static String SP_LOGIN = "edu.missouri.nimh.emotion.LOGIN"; public final static String SP_KEY_LOGIN_STUDY_STARTTIME = "STUDY_START_DAY"; public static int getStudyDay(Context context) { String startStr = getSP(context, SP_LOGIN).getString(SP_KEY_LOGIN_STUDY_STARTTIME, ""); if (!startStr.equals("")) { long start = Long.parseLong(startStr); long current = Calendar.getInstance().getTimeInMillis(); Calendar s = Calendar.getInstance(); s.setTimeInMillis(start); s.set(Calendar.HOUR_OF_DAY, 3); s.set(Calendar.MINUTE, 0); s.set(Calendar.SECOND, 0); s.set(Calendar.MILLISECOND, 0); Calendar c = Calendar.getInstance(); c.setTimeInMillis(current); // c.set(Calendar.HOUR_OF_DAY, 12); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); start = s.getTimeInMillis(); current = c.getTimeInMillis(); return (int) ((current - start) / (24 * 60 * 60 * 1000)); } else { return -1; } } public static SharedPreferences getSP(Context context, String name) { SharedPreferences shp = context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS); return shp; } }