我想将当前日期(YYYY-MM-DD)和时间(SS:MM:HH)的格式更改为'n'几个月前,'n'天前,'n'小时“以前”格式
当前格式:
必填格式:
我使用Bean和Adapter类来获取当前日期。代码如下;
适配器类:
public class MessageAdapter extends BaseAdapter {
private Activity activity;
private List<MessageBean> messageBeanList;
public ImageLoader imageLoader;
private Context context;
public MessageAdapter (Activity activity,List<MessageBean> messageBeanList)
{
super();
this.activity = activity;
// this.context = context;
this.messageBeanList = messageBeanList;
this.context=context;
}
@Override
public int getCount() {
return messageBeanList.size();
}
@Override
public Object getItem(int position) {
return messageBeanList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemHolder itemHolder = new ItemHolder();
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(
R.layout.message_item, null);
imageLoader=new ImageLoader(activity.getApplicationContext());
itemHolder.timestampp = (TextView) convertView
.findViewById(R.id.timestamp);
convertView.setTag(itemHolder);
} else {
itemHolder = (ItemHolder) convertView.getTag();
}
class ItemHolder
{
public TextView timestampp;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to nofify
}
}
BEAN CLASS:
import com.google.gson.annotations.SerializedName;
public class MessageBean {
@SerializedName("date_created")
private String dateCreated = "";
}
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
在SOF中几乎遇到了所有相关的问题,但由于我正在使用bean和适配器类,所以没有得到我想要的东西。如果使用JSON解析,这可以转换日期和时间格式吗?
答案 0 :(得分:1)
我希望这对您有用.. 只需在适配器中添加代码
/***************get current date time function*************/
String curDateTime = "";
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
String curDateTime = df.format(c.getTime());
}catch(Exception e){}
/***************get current date time function*************/
// add simple_date_format for uniqueness
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = simpleDateFormat.parse(messageBean.getDateCreated()); // like 2016-03-09 07:08:27
Date date2 = simpleDateFormat.parse(curDateTime); // 2016-03-09 07:08:27
differentDateTime = printDifference(date1, date2);
} catch (Exception e) {
e.printStackTrace();
}
itemHolder.timestampp.setText(differentDateTime);
/************function of print different for showing date into ago format***************/
//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public String printDifference(Date startDate, Date endDate){
String allDaysMonsSeconds="";
//milliseconds
long different = endDate.getTime() - startDate.getTime();
Log.d("TAG","startDate : " + startDate);
Log.d("TAG","endDate : "+ endDate);
Log.d("TAG","different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long yearInMilli = daysInMilli * 365;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
long elapsedYears = different / yearInMilli;
Log.d("TAG","%d days, %d hours, %d minutes, %d seconds %n, %d years:"+elapsedDays+","+elapsedHours+","+elapsedMinutes+","+elapsedSeconds+","+elapsedYears);
// code for showing before days...
if(elapsedDays<=0){
if (elapsedHours<=0)
{
if (elapsedMinutes<=0)
{
allDaysMonsSeconds = elapsedSeconds+" second ago";
}
else
{
allDaysMonsSeconds = elapsedMinutes+" minute ago";
}
}
else
{
allDaysMonsSeconds = elapsedHours+" hour ago";
}
}
else{
allDaysMonsSeconds = elapsedDays+" day ago";
}
return allDaysMonsSeconds;
}
/************function of print different for showing date into ago format***************/
答案 1 :(得分:0)
试试这个,
首先初始化您的textview。
Random rand = new Random();
int id = rand.nextInt(1000000) + 1;
将以下方法称为
TextView t = new TextView();
答案 2 :(得分:0)
使用以下代码,并在时间中将ago
连接为字符串:
startTime = "2016-03-09 16:23:30";
StringTokenizer tk = new StringTokenizer(startTime);
String date = tk.nextToken();
String time = tk.nextToken();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt;
try {
dt = sdf.parse(time);
System.out.println("Time Display: " + sdfs.format(dt)+" ago"); // <-- I got result here
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 3 :(得分:0)
String curDateTime = "";
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
String curDateTime = df.format(c.getTime());
}catch(Exception e){}
/***************get current date time function*************/
// add simple_date_format for uniqueness
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = simpleDateFormat.parse(messageBean.getDateCreated()); // like 2016-03-09 07:08:27
Date date2 = simpleDateFormat.parse(curDateTime); // 2016-03-09 07:08:27
differentDateTime = printDifference(date1, date2);
} catch (Exception e) {
e.printStackTrace();
}
itemHolder.timestampp.setText(differentDateTime);
/************function of print different for showing date into ago format***************/
//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public String printDifference(Date startDate, Date endDate){
String allDaysMonsSeconds="";
//milliseconds
long different = endDate.getTime() - startDate.getTime();
Log.d("TAG","startDate : " + startDate);
Log.d("TAG","endDate : "+ endDate);
Log.d("TAG","different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long yearInMilli = daysInMilli * 365;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
long elapsedYears = different / yearInMilli;
Log.d("TAG","%d days, %d hours, %d minutes, %d seconds %n, %d years:"+elapsedDays+","+elapsedHours+","+elapsedMinutes+","+elapsedSeconds+","+elapsedYears);
// code for showing before days...
if(elapsedDays<=0){
if (elapsedHours<=0)
{
if (elapsedMinutes<=0)
{
allDaysMonsSeconds = elapsedSeconds+" second ago";
}
else
{
allDaysMonsSeconds = elapsedMinutes+" minute ago";
}
}
else
{
allDaysMonsSeconds = elapsedHours+" hour ago";
}
}
else{
allDaysMonsSeconds = elapsedDays+" day ago";
}
return allDaysMonsSeconds;
}