我想将日期时间转换为
Mon APR 6, 2012 11:12 am
这种格式
我在用
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
此代码获取日期格式,但它返回
Mon Apr 6, 2012 11:12 am
我需要大写月份,所有其他都是一样的。
有什么解决办法,请参考我。
谢谢
我想将日期时间转换为
Mon APR 6, 2012 11:12 am
这种格式
我在用
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
此代码获取日期格式,但它返回
Mon Apr 6, 2012 11:12 am
我需要大写月份,所有其他都是一样的。
有什么解决办法,请参考我。
谢谢
您可以Mon Apr 6, 2012 11:12 am在字符串数组中使用 chatAt() 函数获取和月份作为 chra。然后使用字符串类中的 toUpperCase() 方法将其转换为 uppr
试试这个-
import java.text.SimpleDateFormat;
import java.util.Date;
class Solution
{
public static void main (String[] args)
{
Date d = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
String month = date.substring(4, 7);
date = date.replaceFirst(month, month.toUpperCase());
System.out.println(date);
}
}
但是,如果您的日期格式发生变化,这将不起作用。你需要明白这一点。
试试这个代码
Date d = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
Log.v("Test","Date==="+d);
String[] temp;
String month ;
String final_string_date = "" ;
temp = date.split(" ");
for(int i =0; i < temp.length ; i++)
{
if(i==1)
month=temp[i].toUpperCase();
else
month=temp[i];
final_string_date = final_string_date+" "+ month;
}
Log.v("Test","final_string_date==="+final_string_date.trim());
看看 DateFormatSymbols http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormatSymbols.html
样本 :
DateFormatSymbols symbols = new DateFormatSymbols();
symbols.setShortMonths(new String[]{"JAN","FEB"....});
SimpleDateFormat format = new SimpleDateFormat("EE MMM d, yyyy hh:mm a", symbols);
public static String DateFormat(String repdate,SimpleDateFormat formater )
{
long d = Date.parse(repdate);
String[] arr= formater.format(d).split(" ");
arr[1] = arr[1].toUpperCase();
String date = "";
for(int i=0;i<arr.length;i++)
{
date = date + arr[i] +" ";
}
return date;
}
以 ("EE MMM d, yyyy hh:mm a") 格式传递您当前的日期字符串和日期格式化程序。你会得到你的解决方案。