public class CalendarCal {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yy");
String m = simpleDateformat.format(date);
System.out.println("Year:" + m);
int year = Calendar.getInstance().get(Calendar.YEAR) % 100;
System.err.println(year);
}
}
我可以从中检索当前年份。如何在实用程序的帮助下获得最后一个(上一年)?我不想像currentyear - 1
或类似的那样进行计算。
答案 0 :(得分:16)
只需使用Calendar.add
方法即可添加"添加" -1
年份并将其包装在您自己的实用程序方法中:
private static int getPreviousYear() {
Calendar prevYear = Calendar.getInstance();
prevYear.add(Calendar.YEAR, -1);
return prevYear.get(Calendar.YEAR);
}
public static void main(String[] args) {
System.out.println(getPreviousYear());
}
在我的系统上打印:
2011
答案 1 :(得分:1)
这很简单,如下所述:
//Create calendar instance
Calendar instance = Calendar.getInstance();
//Set your date to it
instance.setTime(date);
//Substract one year from it
instance.add(Calendar.YEAR, -1);
//Use the pattern you wish to display the date
SimpleDateFormat isoFormat = new SimpleDateFormat("dd-MMM-yyyy");
// Convert the date to string using format method
String previousYearDate = isoFormat.format(instance.getTime());
答案 2 :(得分:0)
public static String previousDateString(String dateString)
throws ParseException {
// Create a date formatter using your format string
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
// Parse the given date string into a Date object.
// Note: This can throw a ParseException.
Date myDate = dateFormat.parse(dateString);
// Use the Calendar class to subtract one day
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -1);
// Use the date formatter to produce a formatted date string
Date previousDate = calendar.getTime();
String result = dateFormat.format(previousDate);
return result;
}
答案 3 :(得分:0)
import java.util.*;
public class GetYear {
public static void main(String []arg){
Calendar cal=Calendar.getInstance();
int year=cal.get(Calendar.YEAR)-1;
System.out.println("Current Year: " + year );
}
}
答案 4 :(得分:0)
轻量级解决方案(没有任何对象的直接或间接构造函数调用)。
final class YearUtil {
static final int MILLIS_IN_SECOND = 1000;
static final int SECONDS_IN_MINUTE = 60;
static final int MINUTES_IN_HOUR = 60;
static final int HOURS_IN_DAY = 24;
static final int DAYS_IN_YEAR = 365;
static final long MILLISECONDS_IN_YEAR = (long) MILLIS_IN_SECOND
* SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR;
public static int getCurrentYear() {
return (int) (1970 + System.currentTimeMillis() / MILLISECONDS_IN_YEAR);
}
}
比使用它:
int prevYear = YearUtil.getCurrentYear() - 1;
这些数字太大了,甚至连几年都没有必要打扰(在你的孩子和你孩子的孩子身上......);)
答案 5 :(得分:0)
使用Joda-Time 2.4:
int year = DateTime.now( DateTimeZone.forID( "Europe/Paris" ) ).minusYears( 1 ).getYear();
答案 6 :(得分:0)
Calendar calendarEnd=Calendar.getInstance();
// You can substract from the current Year to get the previous year last dates.
calendarEnd.set(Calendar.YEAR,calendarEnd.get(Calendar.YEAR)-3);
calendarEnd.set(Calendar.MONTH,11);
calendarEnd.set(Calendar.DAY_OF_MONTH,31);
Date endDate=calendarEnd.getTime();
System.out.println("\nEND Date Of this year : "+endDate);
答案 7 :(得分:0)
import java.time.Year;
public class TryTime {
public static void main(String[] args) {
System.out.println(Year.now().minusYears(1));
}
}