简体   繁体   中英

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

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.

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 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM