I need a java class where it knows today's date and put an output return as '060115' if today's date is 06/01/2015 or June 1st, 2015.
Asked
Active
Viewed 62 times
-6
-
1.. and what are you having trouble with. I assume you have attempted to google this first. – Peter Lawrey Jun 01 '15 at 20:39
-
3[This](https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html) is the first result when I google "How to output java date format as 060115". Everything you need to know is there. Please use Google or search first. If you have some code and it isn't working please add it to the question through an edit. – Captain Man Jun 01 '15 at 20:40
1 Answers
0
You can use java Calendar class to get everything about Time and Date:
System.out.println("0"+Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+"0"
+ (Calendar.getInstance().get(Calendar.MONTH)+1) +"0"+
Calendar.getInstance().get(Calendar.YEAR));
Using String Format:
import java.util.Calendar;
public class PrintDate {
public PrintDate(){
//Create this for later
String year =String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); //Keeping the year 2015
//Format the date to keep it in a single String
String date = String.format("0%d0%d%d",
Calendar.getInstance().get(Calendar.DAY_OF_MONTH),
(Calendar.getInstance().get(Calendar.MONTH)+1), //Cause the Calendar has January as (0) i add +1
Integer.valueOf(year.substring(1,4))); //Make it from 2015 to 15 using Substring Method
System.out.println(date);
}
public static void main(String[] args){
new PrintDate();
}
}

crAlexander
- 376
- 1
- 2
- 12