Jump to content

[Java] Need help with formatting a date variable.

Xenti3

So i'm trying to get a date variable to just provide the time. The problem is it's adding the full date including the year even when it is formatted.

Current code:

closeDate = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).parse(date);

Any ideas? Trying to just get the date in HH:MM:SS format. Thanks.

PC Specs: i5 4690K, MSI GTX 970 Gaming, 16GB Crucial Ballistx Tactical at 1866Mhz, MSI Guard Pro, 120GB Crucial MX100, 4TB HGST HDD, NZXT H440 Black/Blue

Phone: Oneplus 3 w/ Moto 360 2nd gen

Consoles: New 3DS XL, Xbox One, Nintendo switch soon hopefully.

Link to comment
Share on other sites

Link to post
Share on other sites

try

// Convert the milliseconds since the epoch into a
// java.util.Date object.
Date instant = new Date( MSEC_SINCE_EPOCH );

// Set up a simple date format, to give the view
// of the date object that we want.
SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm" );

// Define the String, time, to be our formatted
// view of the milliseconds since the epoch.
String time = sdf.format( instant );

// Print out this "view" of MSEC_SINCE_EPOCH.
System.out.println( "Time: " + time );

output

Time: 18:02

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a way to do it using Calendar: 

 

Calendar cal = Calendar.getInstance(); 
String closeDate = new SimpleDateFormat("HH:mm:ss", Local.ENGLISH).format(cal.getTime()); 
System.out.println(closeDate);

//Output is 02:42:26

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×