java 获取系统的日期 ddmmyyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11598851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:43:34  来源:igfitidea点击:

Get system's date ddmmyyyy

javaandroiddate

提问by Xander

It's probably an easy question but how do you get the system date in format as ddmmyyyyin eclipse. So without any letters, spaces or other characters?

这可能是一个简单的问题,但是您如何ddmmyyyy以 eclipse中的格式获取系统日期。所以没有任何字母、空格或其他字符?

回答by JB Nizet

String todayAsString = new SimpleDateFormat("ddMMyyyy").format(new Date());

Eclipse is an IDE. Java is the language. Googling for "format a date in Java" and "current date in Java" would have led you to hundreds of pages telling you the answer. What matters is the language: Java. Not the IDE, which is just a tool to generate and compile Java code.

Eclipse 是一个 IDE。Java是语言。谷歌搜索“在 Java 中格式化日期”和“在 Java 中的当前日期”会引导您找到数百页告诉您答案。重要的是语言:Java。不是 IDE,它只是一个生成和编译 Java 代码的工具。

回答by Waqas

You can use this code, And can change the format to whatever you like.

您可以使用此代码,并且可以将格式更改为您喜欢的任何格式。

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainClass {
    public static void main(String[] args) {
        String pattern = "MMddyyyy";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        // formatting
        System.out.println(format.format(new Date()));
    }
}

回答by Parag Chauhan

Try this:

试试这个:

               Calendar cal= Calendar.getInstance();
               String cal_for_month = cal.get(Calendar.MONTH);
               String cal_for_year = cal.get(Calendar.YEAR);
                String cal_for_day = cal.get(Calendar.DAY_OF_MONTH);