I have two function to calculate date and time.
public static String getFormattedDate(String time) {
long timeUnix = Long.parseLong(time);
Date myDate = new Date(timeUnix * 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d yy");
re开发者_运维问答turn simpleDateFormat.format(myDate);
}
public static String getFormattedTime(String time) {
long timeUnix = Long.parseLong(time);
Date myDate = new Date(timeUnix * 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
return simpleDateFormat.format(myDate);
}
I get the correct output from them which I need.
Jul 12 11(date function) and 12:09:45(time function)
How can I calculate days from this and how to set am/pm with this.
I am trying to set time with am/pm if it is today's date and if it is older than show the day(mon,tue,wed etc.) and if it is more than week old than show "MMM d yy".
For am/pm notation:
SimpleDateFormat dateFormat = new SimpleDateFormat("KK:mm:ss a");
return dateFormat.format(myDate);
KK is hour in am/pm (0-11), while HH is hour in day (0-23)
Calendar cal = Calendar.getInstance();
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
String localTime = date.format(currentLocalTime);
System.out.println(localTime);
AM/PM can be retrieved using:
SimpleDateFormat dateFormat = new SimpleDateFormat("a");
return dateFormat.format(myDate);
There are a few different options for days, best to refer to the SimpleDateFormat documentation to decide which day format you need.
精彩评论