如何获取当前日期以及从当前日期开始的未来30天的日期。

huangapple go评论159阅读模式
英文:

how to get the current date and date for next 30 days from current date

问题

开发者们,希望大家都好。我正在尝试获取当前日期 2020年9月16日,然后想要获得接下来30天的日期。抱歉,我还是新手,所以问题表达可能不太准确。如果你有解决方案,麻烦分享一下;如果有任何问题,也请指出。提前感谢。

英文:

developers, I hope you are well, I am trying to get the current date 16/09/2020 and then after that want to get a date for the next 30 days. Sorry, I am new so don't get a proper question. please if you have a solution then you can share or if any question then you can refer. Thanks in advance.

答案1

得分: 4

我建议您使用现代java.time日期时间 API 和相应的格式化 API(包:java.time.format)来完成此操作。您可以从**教程:日期时间**中了解有关现代日期时间 API 的更多信息。java.util日期时间 API 和 SimpleDateFormat 已过时且容易出错。

如果您的 Android API 级别仍不符合 Java8,请参阅 https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project通过 desugaring 可用的 Java 8+ API

请按照以下方法使用现代日期时间 API 进行操作:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // 为您的自定义模式定义格式化程序
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        // 我已经使用了您的 JVM 的默认时区。根据您的要求,您可以
        // 更改为不同的时区,例如在注释代码中所示
        // LocalDate today = LocalDate.now(ZoneId.of("Europe/London"));
        LocalDate today = LocalDate.now();// 与 LocalDate.now(ZoneId.systemDefault()) 相同
        // 以默认模式打印,即以 LocalDate.toString() 返回的模式
        System.out.println("Today in the default pattern: " + today);
        // 以定义的自定义模式打印
        System.out.println("Today in dd/MM/yyyy pattern: " + today.format(formatter));

        // 从今天开始的 30 天后
        LocalDate after30Days = today.plusDays(30);
        System.out.println("After 30 days: " + after30Days.format(formatter));

        // 从今天开始的一个月后
        LocalDate afterOneMonth = today.plusMonths(1);
        System.out.println("After one month: " + afterOneMonth.format(formatter));
    }
}

输出:

Today in the default pattern: 2020-09-16
Today in dd/MM/yyyy pattern: 16/09/2020
After 30 days: 16/10/2020
After one month: 16/10/2020
英文:

I recommend you do it using the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time. The java.util date-time API and SimpleDateFormat are outdated and error-prone.

If your Android API level is still not compliant with Java8, check https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project and Java 8+ APIs available through desugaring.

Do it as follows using the modern date-time API:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
	public static void main(String[] args) {
		// Define a formatter for your custom pattern
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

		// I've used the default time-zone of your JVM. As per your requirement, you can
		// change to different time-zone e.g as shown in the commented code
		// LocalDate today = LocalDate.now(ZoneId.of("Europe/London"));
		LocalDate today = LocalDate.now();// Same as LocalDate.now(ZoneId.systemDefault())
		// Print in default pattern i.e. in the pattern returned by LocalDate.toString()
		System.out.println("Today in the default pattern: " + today);
		// Print in the defined custom pattern
		System.out.println("Today in dd/MM/yyyy pattern: " + today.format(formatter));

		// After 30 days from today
		LocalDate after30Days = today.plusDays(30);
		System.out.println("After 30 days: " + after30Days.format(formatter));

		// After 30 days from today
		LocalDate afterOneMonth = today.plusMonths(1);
		System.out.println("After one month: " + afterOneMonth.format(formatter));
	}
}

Output:

Today in the default pattern: 2020-09-16
Today in dd/MM/yyyy pattern: 16/09/2020
After 30 days: 16/10/2020
After one month: 16/10/2020

答案2

得分: 3

这是您可以使用 java.time 完成的方式,现在通过Android API Desugaring可在较低的Android API版本中使用:

public static void main(String[] args) {
    // 获取今天的日期
    LocalDate today = LocalDate.now();
    // 获取30天后的日期
    LocalDate thirtyDaysInFuture = today.plusDays(30);
    // 获取一个月后的日期(不一定与30天后的日期相同)
    LocalDate aMonthInFuture = today.plusMonths(1);
    // 定义输出的期望格式(在Android中使用 log(...) 而不是 System.out)
    DateTimeFormatter customDtf = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    // 使用上面定义的格式化程序打印结果
    System.out.println("今天:\t\t\t" + today.format(customDtf));
    System.out.println("30天后:\t\t" + thirtyDaysInFuture.format(customDtf));
    System.out.println("一个月后:\t" + aMonthInFuture.format(customDtf));
}

输出:

今天:				16/09/2020
30天后:			16/10/2020
一个月后:		16/10/2020
英文:

This is how you could do it using java.time, which is available to lower Android API versions now via Android API Desugaring:

public static void main(String[] args) {
	// get the date of today
	LocalDate today = LocalDate.now();
	// get the date that is 30 days later
	LocalDate thirtyDaysInFuture = today.plusDays(30);
	// get the date a month later (not necessarily the same date as 30 days later)
	LocalDate aMonthInFuture = today.plusMonths(1);
	// define a desired format for output (use log(...) in Android instead of System.out)
	DateTimeFormatter customDtf = DateTimeFormatter.ofPattern("dd/MM/uuuu");
	// print the results using the formatter defined above
	System.out.println("Today:\t\t\t" + today.format(customDtf));
	System.out.println("Thirty days later:\t" + thirtyDaysInFuture.format(customDtf));
	System.out.println("One month later:\t" + aMonthInFuture.format(customDtf));
}

Output:

Today:				16/09/2020
Thirty days later:	16/10/2020
One month later:	16/10/2020

huangapple
  • 本文由 发表于 2020年9月16日 20:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63920463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定