SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
SimpleDateFormat sdff = new SimpleDateFormat("MMM d HH:mm:ss");
System.out.println(sdff.format(new Date()));
Output is:
Aug 04 10:58:55
Aug 4 10:58:55
I want output to be:
Aug 04 10:58:55
Aug 4 10:58:55
Any ideas?
But if day has two numbers I need it to have one space:
Aug 14 10:58:55
Aug 4 10:58:55
Java SimpleDateFormat ExampleString pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);
Create Simple Date Format // Date Format In Java String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); According to the example above, the String pattern will be used to format dates, and the output will be shown as "yyyy-MM-dd".
Creating A Simple Date Format SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.
If you are bother about white space just add a white space to formatter.
"MMM d HH:mm:ss"
Eg:
Calendar calendar=Calendar.getInstance();
int day=calendar.get(Calendar.DAY_OF_MONTH);
SimpleDateFormat sdf;
if(day>9){
sdf = new SimpleDateFormat("MMM dd HH:mm:ss");
}else{
sdf = new SimpleDateFormat("MMM d HH:mm:ss");
}
System.out.println(sdf.format(new Date()));
Out put:
Aug 4 15:50:25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With