Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 日期格式以匹配电话设置中的日期格式_Android_Date_Simpledateformat - Fatal编程技术网

Android 日期格式以匹配电话设置中的日期格式

Android 日期格式以匹配电话设置中的日期格式,android,date,simpledateformat,Android,Date,Simpledateformat,我有一个字符串,返回日期和时间为2012-11-08 12:45:30。我需要以单独的字符串获取日期和时间,然后日期必须以手机日期和时间设置中的格式显示。 以下是我迄今为止尝试过的代码: db的日期值为2012-11-08 我得到的日期格式在手机的设置为 String datefrmt = Settings.System.getString( context.getContentResolver(), Settings.System.DATE_FORMAT);

我有一个字符串,返回日期和时间为2012-11-08 12:45:30。我需要以单独的字符串获取日期和时间,然后日期必须以手机日期和时间设置中的格式显示。 以下是我迄今为止尝试过的代码: db的日期值为2012-11-08 我得到的日期格式在手机的设置为

String datefrmt = Settings.System.getString(
                context.getContentResolver(), Settings.System.DATE_FORMAT);
将此格式应用于从db获取的日期的代码为:

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

我得到了正确的月份和日期,但是年份返回了一些随机值,这不是一个正确的值。谁能告诉我哪里出了问题。谢谢

您可以将simpleDateFormat用作:

 SimpleDateFormat simpDate = new SimpleDateFormat(datefrmt,
                    Locale.ENGLISH);
  String s = simpDate.format(new Date());
作为参考,您可以参考开发者网站:

由于数据库日期为yyyy MM dd,请不要使用动态格式获取日期,否则将导致Hrow parse exception崩溃,请使用以下静态格式获取日期。您可以使用动态格式转换字符串,如下所示:

   //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);
如果您有2012-11-08 12:45:30字符串,并且希望将其解析为Date对象,并将其格式更改为其他系统格式,例如,或2012-11-08格式:


由于最终字符串格式=Settings.System.getStringgetActivity.getContentResolver,Settings.System.DATE\u格式//可能为空 因此,出于一般目的,我对我的函数进行了如下定制

@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());
}

datefrmt的值是多少?它将在手机的日期和时间设置中显示日期格式@YogendraSingh@Rosalie让我换一种方式问一下,在同一个环境下,你会得到不同的年份吗?如果否,当您获得不同年份时,设置的datefrmt值是什么,例如MM/dd/yyyy?当我应用日期格式时,它会崩溃。手机中的日期格式为MM/dd/yyyy@yogendrasingh当应用手机设置格式时,我得到的值为08-11-0170。我刚才说的电话设置是MM dd yyyy。db的值为2012-11-03@约根德拉Singh@Rosalie第一个改进。它没有崩溃。请您打印日志日期_1,以了解导致问题的两种转换中的哪一种?@Rosalie我验证过。如果我通过硬编码日期FRMT MM dd yyyy,它将打印我11-03-2012。我还建议您记录datefrmt,以确保其格式字符串正确。日期格式在日志中是正确的,如MM dd yyyy,但应用后得到的值是08-11-0170。不知道它在哪里coming@Rosalie当前位置就我的理解而言,这个答案与您接受的答案有何不同?
@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());
}