1

I have a string which returns the date and time as 2012-11-08 12:45:30 . I need to get the date and time in separate strings and then the date has to be shown in the format which is there in the Phone's Date and Time settings. Here is the code which I have tried so far: date value from db is 2012-11-08 I am getting the date format in phone's settings as

String datefrmt = Settings.System.getString(
                context.getContentResolver(), Settings.System.DATE_FORMAT);

the code to apply this format to the obtained date from db is:

SimpleDateFormat sdf = new SimpleDateFormat(datefrmt);
java.util.Date date_1 = sdf.parse("2012-11-08");
String s = sdf.format(date_1);

I am getting the month and day properly but the year its returning something randomly and thats not a correct value. Can anyone please guide me where am I going wrong. Thanks

Lavanya
  • 3,903
  • 6
  • 31
  • 57

4 Answers4

0

you can use the simpleDateFormat as:

 SimpleDateFormat simpDate = new SimpleDateFormat(datefrmt,
                    Locale.ENGLISH);
  String s = simpDate.format(new Date());

for reference, you can refer to developer site:

for more information refer this link

Venkat
  • 3,447
  • 6
  • 42
  • 61
0

Since your DB date is in yyyy-MM-dd, don't use the dynamic format to get the date, otherwise it will crash(throw parse exception), instead use the static format as below to get the date. You may use the dynamic format to convert the string as below:

   //use static format to convert into date from static formatted data in db 
   SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
   java.util.Date date_1 = sdf1.parse("2012-11-08");//got the date

   //use the dynamic format to convert into string
   SimpleDateFormat sdf2 = new SimpleDateFormat(datefrmt);
   String s = sdf2.format(date_1);
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • when applying the phone setting format I am getting the value as 08-11-0170. The phone setting as I said has MM-dd-yyyy. The value from db is 2012-11-03. @Yogendra Singh – Lavanya Nov 08 '12 at 06:36
  • @Rosalie **First improvement. Its not crashing.** Can you please print(log) date_1 to know, which of the two conversions causing issue? – Yogendra Singh Nov 08 '12 at 06:40
  • @Rosalie I verified. If I pass hard coded datefrmt `MM-dd-yyyy`, it prints me `11-03-2012`. Also I advice you to log datefrmt to make sure, its having right format string. – Yogendra Singh Nov 08 '12 at 06:44
  • the date format is correct in log as MM-dd-yyyy but the value I'm getting after applying is 08-11-0170. Don't know from where it is coming – Lavanya Nov 08 '12 at 07:13
  • @Rosalie: Just for my understanding, how was this answer different that one you accepted? – Yogendra Singh Nov 08 '12 at 17:16
  • it was a detail answer without any crashes or any wrong data. @Yogendra Singh – Lavanya Nov 09 '12 at 04:32
  • @Rosalie: :) When I read it, I still don't see any difference even in details. Same tow format Strings and same two conversions. – Yogendra Singh Nov 09 '12 at 04:36
0

If you have 2012-11-08 12:45:30 string and you want to parse it to Date object and change its format to another (system format for example or 2012-11-08 format):

try {
        String yourString="2012-11-08 12:45:30";

        //first we parse input date string and create Date object
        String inputDateString ="yyyy-dd-MM hh:mm:ss";
        SimpleDateFormat inputDateFormat = new SimpleDateFormat(inputDateString);
        java.util.Date  date = inputDateFormat.parse(yourString);

        //log
        Log.e(getClass().getName(), inputDateFormat.format(date));

        //create system date format 
        String dateformt = Settings.System.getString( this.getContentResolver(), Settings.System.DATE_FORMAT);
        //if its for some reason==null let it be "yyyy-dd-MM" 
        if(dateformt==null){
            dateformt="yyyy-dd-MM";
        }
        SimpleDateFormat systemDateFormat = new SimpleDateFormat(dateformt);
        String outputString = systemDateFormat.format(date);

        //log
        Log.e(getClass().getName(), outputString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
ashakirov
  • 12,112
  • 6
  • 40
  • 40
0

Since final String format = Settings.System.getString(getActivity().getContentResolver(), Settings.System.DATE_FORMAT);//may be NULL
So I customized my function as below for general purposes

@SuppressLint("SimpleDateFormat")
public String getCurrentShortDateFormat() { 
    final String[] SFs = new String [] {
            "MM/dd/yyyy",
            "dd/MM/yyyy",
            "yyyy/MM/dd"
    }; 

    final Date date = new Date(24638400000L);//24638400000=Oct/13/1970; 0==Jan/1/1970
    java.text.DateFormat shortDateFormat = android.text.format.DateFormat.getDateFormat(getActivity().getApplicationContext());      
    String current = shortDateFormat.format(date);
    SimpleDateFormat which = new SimpleDateFormat();
    for (int i = 0; i < SFs.length; i++) {
        which.applyPattern(SFs[i]);
        if (which.format(date).compareTo(current)==0)
            return SFs[i];
    }
    return null;
}

@Override
public void onResume() {
    super.onResume(); 
    Log.d(LOG_TAG, "----- onResume.CurrentShortDateFormat-----" + getCurrentShortDateFormat());
}
coolscott
  • 1
  • 1