在我的应用程序中,我收到了内容的发布日期,并以MMM,dd
格式显示。
它工作得很好,但现在我想根据发布的日期显示日期格式。
19:45
)Sunday, 14:30
)MM/dd/yyyy HH:mm:ss
。为此我尝试了这段代码:
public static String formateDate( String inputDate )
{
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" );
String lDate = null;
Date date = new Date();
SimpleDateFormat dateFormater =null;
try
{
date = dateFormat.parse( inputDate );
Log.d("debug", "Published Date" + date);
int comparision=diff(date);
Log.d("debug", "Date Comparision" + comparision);
if(comparision==0)
{
dateFormater = new SimpleDateFormat( "kk:mm");
}
else if(comparision>0 && comparision<=7)
{
dateFormater = new SimpleDateFormat( "EEEE kk:mm");
}
else if(comparision > 7)
{
dateFormater = new SimpleDateFormat( "MMM.dd.yyyy kk:mm" );
}
//dateFormater = new SimpleDateFormat( "MMM,dd" );
lDate = dateFormater.format( date );
}
catch( Exception e )
{
e.printStackTrace();
}
return lDate;
}
我在这里计算天数之间的差异
public static int diff( Date date1)
{
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
Date todayDate=new Date();
c1.setTime( date1 );
c2.setTime( todayDate );
int diffDay = 0;
if( c1.before( c2 ) )
{
diffDay = countDiffDay( c1,
c2 );
}
else
{
diffDay = countDiffDay( c2,
c1 );
}
return diffDay;
}
通过这种方式,我能够以某种方式得到我期待的东西。但我认为有一种可行的方法而不是这种方法。任何人都可以指出这个吗?
答案 0 :(得分:0)
long currtime= System.currentTimeMillis();
Calendar calander = Calendar.getInstance();
calander .setTimeInMillis(currtime);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
//above line of SDF will give you output in 24 hour format
String dateforrow = dateFormat.format(calander.getTime());
SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
//above line of SDF will give you output in 12 hour format
String dateforrow2 = dateFormat2.format(calander.getTime());