Android Open Source - amulet Date And Time






From Project

Back to project page amulet.

License

The source code is released under:

MIT License

If you think the Android project amulet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package scamell.michael.amulet;
//from w w  w.  java2s  . co  m
import android.content.Context;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateAndTime {

//    public static String getDateAndTimeNow() {
//        String dateFormatString = "dd-MM-yyyy HH:mm:ss";
//
//        DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
//        Date dateNow = new Date();
//        return dateFormat.format(dateNow);
//    }

    /**
     * Date Formats:
     * http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
     */
    public static String getDateAndTimeNowForTasks() {
        String dateFormatString = "yyyy-MM-dd HH:mm:ss.SSS";

        DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
        Date dateNow = new Date();
        return dateFormat.format(dateNow);
    }

    public static String setDateAndTimeForApp(String dateToConvert) {
        String newDate = null;
        dateToConvert = dateToConvert.replace("T", " ");
        SimpleDateFormat convertFromDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat convertToDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        try {
            Date date = convertFromDate.parse(dateToConvert);
            newDate = convertToDate.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            Log.e("ERROR_PARSING_DATE", "Couldn't parse date from server");
        }
        return newDate;
    }

    private static String getDateSuffix(int day) {
        switch (day) {
            case 1:
            case 21:
            case 31:
                return ("st");
            case 2:
            case 22:
                return ("nd");
            case 3:
            case 23:
                return ("rd");
            default:
                return ("th");
        }
    }

    public static String getDateAndTime(Context context) {
        String dateString = SharedPreferencesWrapper.getFromPrefs(context, "last_date_played", "Today");
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdfTimeDay = new SimpleDateFormat("h:mmaaa, d");
        SimpleDateFormat sdfMonthYear = new SimpleDateFormat(" MMMM, yyyy");
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("HOME_FRAGMENT", "Couldn't parse date string or no previous date");
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dateSuffix = DateAndTime.getDateSuffix(day);
        String timeDay = sdfTimeDay.format(date);
        //http://stackoverflow.com/questions/13581608/displaying-am-and-pm-in-small-letter-after-date-formatting
        timeDay = timeDay.replace("AM", "am").replace("PM", "pm");
        String monthYear = sdfMonthYear.format(date);
        return timeDay + dateSuffix + monthYear;
    }
}




Java Source Code List

scamell.michael.amulet.AboutDialogFragment.java
scamell.michael.amulet.AccountDetailsFragment.java
scamell.michael.amulet.ChangePasswordDialogFragment.java
scamell.michael.amulet.CoolveticaTextView.java
scamell.michael.amulet.DateAndTime.java
scamell.michael.amulet.DrinkDiaryEntries.java
scamell.michael.amulet.DrinkDiaryEntryAdapter.java
scamell.michael.amulet.DrinkDiaryEntry.java
scamell.michael.amulet.DrinkDiaryFavouriteDrinksDialogFragment.java
scamell.michael.amulet.DrinkDiaryFragment.java
scamell.michael.amulet.ExampleDrinksDialogFragment.java
scamell.michael.amulet.FavouriteDrinkUtility.java
scamell.michael.amulet.HomeFragment.java
scamell.michael.amulet.InspectionTaskActivity.java
scamell.michael.amulet.InspectionTaskInstructions1Fragment.java
scamell.michael.amulet.InspectionTaskInstructions2Fragment.java
scamell.michael.amulet.LastTaskInstructionFragment.java
scamell.michael.amulet.LoginActivity.java
scamell.michael.amulet.LogoutDialogFragment.java
scamell.michael.amulet.MainActivity.java
scamell.michael.amulet.OnPostHttpData.java
scamell.michael.amulet.OnRetrieveHttpData.java
scamell.michael.amulet.PostHTTPDataAsync.java
scamell.michael.amulet.RegisterActivity.java
scamell.michael.amulet.ResumeTaskDialogFragment.java
scamell.michael.amulet.RetrieveHTTPDataAsync.java
scamell.michael.amulet.RetrieveHTTPData.java
scamell.michael.amulet.RetrieveTaskDataFromServer.java
scamell.michael.amulet.SaveDrinkDiaryUtility.java
scamell.michael.amulet.SendDrinkDiaryEntriesToServer.java
scamell.michael.amulet.SendDrinkDiaryEntryToServer.java
scamell.michael.amulet.SendTaskEntriesToServer.java
scamell.michael.amulet.SendTaskEntryToServer.java
scamell.michael.amulet.SequenceTaskActivity.java
scamell.michael.amulet.SequenceTaskInstruction1Fragment.java
scamell.michael.amulet.SequenceTaskInstruction2Fragment.java
scamell.michael.amulet.SequenceTaskInstruction3Fragment.java
scamell.michael.amulet.SharedPreferencesWrapper.java
scamell.michael.amulet.Sound.java
scamell.michael.amulet.TaskEntries.java
scamell.michael.amulet.TaskEntry.java
scamell.michael.amulet.TaskFinishFragment.java
scamell.michael.amulet.TaskInstructionsActivity.java
scamell.michael.amulet.TaskStartCountdownFragment.java
scamell.michael.amulet.TasksFirstStartWelcomeFragment.java
scamell.michael.amulet.TasksMenuFragment.java
scamell.michael.amulet.UnitCalculatorFavouriteDrinksDialogFragment.java
scamell.michael.amulet.UnitCalculatorFragment.java
scamell.michael.amulet.UnitCalculator.java