JAVA 日期 INT变量小于10时,怎么给它补0,求解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 日期 INT变量小于10时,怎么给它补0,求解相关的知识,希望对你有一定的参考价值。

int year;
int month;
int day;

GregorianCalendar timenow=new GregorianCalendar();
year=timenow.get(Calendar.YEAR);
month=timenow.get(Calendar.MONTH)+1;
day=timenow.get(Calendar.DAY_OF_MONTH);

String alltime=year+"-"+month+"-"+day;

怎么让其变为“xxxx-yy-zz”的格式??

参考技术A 给你个例子吧

import java.text.*;
import java.util.*;

public class GetTime
public static void main(String[] args)
MyThread mt = new MyThread();
mt.start();



class MyThread extends Thread

public void run()
while(true)
Date d=new Date();//获取时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");//转换格式
System.out.println(sdf.format(d));//打印

try
Thread.sleep(1000);
catch(InterruptedException e)
e.printStackTrace();




本回答被提问者采纳
参考技术B SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cc = Calendar.getIntance();
String time = sdf.parse(cc.getTime());
参考技术C public static void main(String[] args)
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String alltime = format.format(d);
System.out.println(alltime);

以上是关于JAVA 日期 INT变量小于10时,怎么给它补0,求解的主要内容,如果未能解决你的问题,请参考以下文章

java中如何定义常量

变量不存储给它的东西 - Swift 3

c语言:从键盘上输入一个日期,判断输入的日期是不是正确

java中怎么比较两个日期的大小

c++ 当前时间和日期获取

Java 根据出生日期获得年龄