如何从字符串中获取经过的年数
How to obtain elapsed years from string
我有字符串日期“3.9.1991”。我想获得自该日期以来已经过去了多少年。例如 23. 我如何使用日历或日期来实现它?
编辑:
我一直在尝试这个:
private String parseVkBirthday(String birthdayString) {
// 3.9.1991
SimpleDateFormat formatter = new SimpleDateFormat("d.M.yyyy");
String formattedDate = null;
Calendar date = Calendar.getInstance();
int year = 0;
try {
Date currentDate = new Date();
Date birthdayDate = formatter.parse(birthdayString);
Log.d(LOG_TAG, "year1 = " + currentDate.getTime() + " year2 = " + birthdayDate.getTime());
long diff = currentDate.getTime() - birthdayDate.getTime();
birthdayDate.setTime(diff);
date.setTime(birthdayDate);
year = date.get(Calendar.YEAR);
} catch (ParseException e) {
e.printStackTrace();
}
return "" + year;
}
但它 returns 我 1993.
答案:
有 3 种方法可以解决这个问题:
1) 正如 codeaholicguy 回答的那样,我们可以使用 Joda-Time 库(我更喜欢 Android)。
2) 正如 Basil Bourque 回答的那样,我们可以使用 ThreeTen-Backport 库来使用 java.time 类 来自 java 8.
3) 我们可以使用 java 8 和 java.time 中的 类。
谢谢大家。
Calendar mydate = new GregorianCalendar();
String mystring = "3.9.1991";
Date thedate = new SimpleDateFormat("d.m.yyyy", Locale.ENGLISH).parse(mystring);
mydate.setTime(thedate);
//breakdown
System.out.println("year -> "+mydate.get(Calendar.YEAR));
Reference
使用SimpleDateFormat
and Period
of Joda-Time库,示例如下:
String pattern = "dd.MM.yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date date = format.parse("3.9.1991");
System.out.println(date);
Period period = new Period(date.getTime(), (new Date()).getTime());
System.out.println(period.getYears());
String fullDate="3.9.1991";
String[] splitDate=fullDate.split(".");
int year=Integer.parseInt(splitDate[2]);
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int passedYears=currentYear-year;
Calendar.YEAR 可用于从当前日期添加或减去年份,其方式与我们在日期中添加日期和月份的方式相同。
示例程序:
import java.util.Calendar;
public class Years {
public static void main(String[] args) {
//create Calendar instance
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//add year to current date using Calendar.add method
now.add(Calendar.YEAR,1);
System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//substract year from current date
now =Calendar.getInstance();
now.add(Calendar.YEAR,-100);
System.out.println("date before 100 years : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
}
}
http://forgetcode.com/Java/1568-Adding-or-Subtracting-Years-to-Current-Date#
基于 with the Joda-Time 2.8 库:
// get the current year with @xrcwm's code
Calendar mydate = new GregorianCalendar();
String mystring = "3.9.1991";
Date thedate = new SimpleDateFormat("d.m.yyyy", Locale.ENGLISH).parse(mystring);
DateTime myDateTime = new DateTime(thedate.getTime()); // joda DateTime object
// get he current date
DateTime currentDateTime = new DateTime();
// get the years value
long years = Years.between(currentDateTime, myDateTime).getYears()
上面的代码应该会给你正确的值。请注意,这段代码可能有一些语法错误。
附带说明一下,Java 8 有一个 time
包,它似乎提供了更多相同的功能。
java.time
Java 8 和更高版本中的新 java.time package 取代了旧的 java.util.Date/.Calendar & SimpleTextFormat classes.
首先使用新的 DateTimeFormatter class 解析字符串。不要使用 SimpleTextFormat。并阅读文档,因为新旧 classes.
之间的符号代码可能存在细微差异
获取今天的日期,计算经过的年数。请注意,我们需要一个时区。时区对于确定日期至关重要。例如,巴黎的新一天比蒙特利尔早。
Period
class 将时间跨度视为与时间轴上的任何点无关的年、月和日数。
between
方法使用日期时间处理常用的 "Half-Open" 方法。开头是包含,结尾是不包含。
java.time 的默认格式遵循 ISO 8601 标准。如果您希望使用不同的字符串表示日期时间值,请应用格式化程序。
String input = "3.9.1991" ;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.yyyy") ;
LocalDate then = LocalDate.parse( input, formatter ) ;
ZoneId zone = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( zone ) ; // Specify time zone to get your locality’s current date.
Period period = Period.between( then , today ) ;
int years = period.getYears() ;
System.out.println( "Between " + then + " and " + today + " is " + years + " years.");
Between 1991-09-03 and 2015-07-09 is 23 years.
乔达时间
Android 目前缺少 Java 8 个功能。所以你不能使用java.time。除非 ThreeTen-Backport 项目 (a) 支持上面示例中使用的 classes 并且 (b) 在 Android 上工作(我不知道)。
或者,您可以使用 Joda-Time,启发了 java.time 的第三方库。上述代码示例的 Joda-Time 代码版本非常相似。在这种情况下,java.time 和 Joda-Time 彼此平行,具有相似的 classes.
我有字符串日期“3.9.1991”。我想获得自该日期以来已经过去了多少年。例如 23. 我如何使用日历或日期来实现它?
编辑:
我一直在尝试这个:
private String parseVkBirthday(String birthdayString) {
// 3.9.1991
SimpleDateFormat formatter = new SimpleDateFormat("d.M.yyyy");
String formattedDate = null;
Calendar date = Calendar.getInstance();
int year = 0;
try {
Date currentDate = new Date();
Date birthdayDate = formatter.parse(birthdayString);
Log.d(LOG_TAG, "year1 = " + currentDate.getTime() + " year2 = " + birthdayDate.getTime());
long diff = currentDate.getTime() - birthdayDate.getTime();
birthdayDate.setTime(diff);
date.setTime(birthdayDate);
year = date.get(Calendar.YEAR);
} catch (ParseException e) {
e.printStackTrace();
}
return "" + year;
}
但它 returns 我 1993.
答案:
有 3 种方法可以解决这个问题:
1) 正如 codeaholicguy 回答的那样,我们可以使用 Joda-Time 库(我更喜欢 Android)。
2) 正如 Basil Bourque 回答的那样,我们可以使用 ThreeTen-Backport 库来使用 java.time 类 来自 java 8.
3) 我们可以使用 java 8 和 java.time 中的 类。
谢谢大家。
Calendar mydate = new GregorianCalendar();
String mystring = "3.9.1991";
Date thedate = new SimpleDateFormat("d.m.yyyy", Locale.ENGLISH).parse(mystring);
mydate.setTime(thedate);
//breakdown
System.out.println("year -> "+mydate.get(Calendar.YEAR));
Reference
使用SimpleDateFormat
and Period
of Joda-Time库,示例如下:
String pattern = "dd.MM.yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date date = format.parse("3.9.1991");
System.out.println(date);
Period period = new Period(date.getTime(), (new Date()).getTime());
System.out.println(period.getYears());
String fullDate="3.9.1991";
String[] splitDate=fullDate.split(".");
int year=Integer.parseInt(splitDate[2]);
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int passedYears=currentYear-year;
Calendar.YEAR 可用于从当前日期添加或减去年份,其方式与我们在日期中添加日期和月份的方式相同。
示例程序:
import java.util.Calendar;
public class Years {
public static void main(String[] args) {
//create Calendar instance
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//add year to current date using Calendar.add method
now.add(Calendar.YEAR,1);
System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//substract year from current date
now =Calendar.getInstance();
now.add(Calendar.YEAR,-100);
System.out.println("date before 100 years : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
}
}
http://forgetcode.com/Java/1568-Adding-or-Subtracting-Years-to-Current-Date#
基于
// get the current year with @xrcwm's code
Calendar mydate = new GregorianCalendar();
String mystring = "3.9.1991";
Date thedate = new SimpleDateFormat("d.m.yyyy", Locale.ENGLISH).parse(mystring);
DateTime myDateTime = new DateTime(thedate.getTime()); // joda DateTime object
// get he current date
DateTime currentDateTime = new DateTime();
// get the years value
long years = Years.between(currentDateTime, myDateTime).getYears()
上面的代码应该会给你正确的值。请注意,这段代码可能有一些语法错误。
附带说明一下,Java 8 有一个 time
包,它似乎提供了更多相同的功能。
java.time
Java 8 和更高版本中的新 java.time package 取代了旧的 java.util.Date/.Calendar & SimpleTextFormat classes.
首先使用新的 DateTimeFormatter class 解析字符串。不要使用 SimpleTextFormat。并阅读文档,因为新旧 classes.
之间的符号代码可能存在细微差异获取今天的日期,计算经过的年数。请注意,我们需要一个时区。时区对于确定日期至关重要。例如,巴黎的新一天比蒙特利尔早。
Period
class 将时间跨度视为与时间轴上的任何点无关的年、月和日数。
between
方法使用日期时间处理常用的 "Half-Open" 方法。开头是包含,结尾是不包含。
java.time 的默认格式遵循 ISO 8601 标准。如果您希望使用不同的字符串表示日期时间值,请应用格式化程序。
String input = "3.9.1991" ;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.yyyy") ;
LocalDate then = LocalDate.parse( input, formatter ) ;
ZoneId zone = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( zone ) ; // Specify time zone to get your locality’s current date.
Period period = Period.between( then , today ) ;
int years = period.getYears() ;
System.out.println( "Between " + then + " and " + today + " is " + years + " years.");
Between 1991-09-03 and 2015-07-09 is 23 years.
乔达时间
Android 目前缺少 Java 8 个功能。所以你不能使用java.time。除非 ThreeTen-Backport 项目 (a) 支持上面示例中使用的 classes 并且 (b) 在 Android 上工作(我不知道)。
或者,您可以使用 Joda-Time,启发了 java.time 的第三方库。上述代码示例的 Joda-Time 代码版本非常相似。在这种情况下,java.time 和 Joda-Time 彼此平行,具有相似的 classes.