1. Get the current time
Ruby Code
- Calendar cal = Calendar.getinstance ();
- String Time=formatter.format (Cal.gettime ());
- String strend=time.substring (0, 16);//2007-10-30 09:30
2. Get the day before
Ruby Code
- Calendar C = calendar.getinstance ();
- C.add (Calendar.day_of_month,-1);
- SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- String Mdatetime=formatter.format (C.gettime ());
- String strstart=mdatetime.substring (0, 16);//2007-10-29 09:30
3. Get the time of the previous days
Ruby Code
- Calendar C = calendar.getinstance ();
- C.add (Calendar.day_of_month,-5);//Get Top 5
- SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- String Mdatetime=formatter.format (C.gettime ());
- String strstart=mdatetime.substring (0, 16);//2007-10-24 09:30
4. Get the time of the previous years of the current time
Ruby Code
- Calendar C = calendar.getinstance ();
- C.add (Calendar.year,-5);//Get the first 5 years
- SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- String Mdatetime=formatter.format (C.gettime ());
- String strstart=mdatetime.substring (0, +);//2002-10-24 09:30
From: http://heiwuya.iteye.com/blog/294174
How to use Java to get the current time and the day before