Time format conversion



1.Calendar 转化 String 
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());




2.String 转化Calendar
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
 
 
3.Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
 
 
4.String 转化Date 
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse(str);
 
 
5.Date converts Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());
 
6.Calendar converts Date
Calendar calendar = Calendar.getInstance( );
java.util.Date date =calendar.getTime();


7. Get the complete time in the database
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp newdate=rs.getTimestamp(" CREATETIME");
Date date=new Date(newdate.getTime());
String pecentTime = sdf.format(date);//Current time
practice.setCreateTime(pecentTime);


8. Get the time before and after the current time
public Date getBeforeOrAfterDay(Date date,long day){
Calendar startCal = Calendar.getInstance(); 
startCal.setTimeInMillis(DateUtil.getMillis(date) + ((long) day) * 24 * 3600 * 1000);
Date beforeDay = startCal.getTime();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
beforeDay = DateUtil.parseDate(dateFormater.format(beforeDay), "yyyy/MM/dd");
return beforeDay; } 9. Get the first and last day of the current month Calendar c = Calendar.getInstance();   c. setTime(date); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH, 1);//Set to No. 1, the current date is the first day of the month //Get the last day of the current month Calendar ca = Calendar.getInstance(); ca.setTime(date); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); 












SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("start==================="+c.getTime());
System.out.println("end==================="+ca.getTime());
System.out.println("start=================="+sdf.format(c.getTime()));
System.out.println("end=================="+sdf.format(ca.getTime()));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325757706&siteId=291194637