for example if the user input is 1.71 (1 hour and 71 minutes), then i want to convert it to 2.11 (2 hours and 11 minutes)
DecimalFormat time = new DecimalFormat("##.## p.m");
int userInput = 1.71;
System.out.print(time.format(userInput));
for example if the user input is 1.71 (1 hour and 71 minutes), then i want to convert it to 2.11 (2 hours and 11 minutes)
DecimalFormat time = new DecimalFormat("##.## p.m");
int userInput = 1.71;
System.out.print(time.format(userInput));
Parse the input as date and than format the date to your output format:
Scanner sc = new Scanner(System.in);
DateFormat df = new SimpleDateFormat("HH.mm");
DateFormat dfout = new SimpleDateFormat("H.mm a");
Date date = df.parse(sc.next());
System.out.println(dfout.format(date));
I'm not sure whether I get your question right, but shouldn't the following work?
int hours = (int)userInput;
decimal minutestmp = ((userInput - hours) * 60);
int minutes = (int)minutestmp;
String TimeString = hours + ":" + minutes;
Date date = new SimpleDateFormat("HH:mm").parse(TimeString);
String newTimeString = new SimpleDateFormat("H:mm").format(date);
The ISO 8601 standard for date-time strings defines a format for what they call Durations: PnYnMnDTnHnMnS
.
The Joda-Time library knows how to parse and generate such strings.
So first, transform your input into such a string. Then feed that new string into Joda-Time. Lastly ask Joda-Time to recalculate the hours and minutes. All of this has been covered on StackOverflow multiple times. Please search for details.
To translate that input, replace the period, and prepend and append.
String d = "PT" + "1.71".replace( ".", "H") + "M"; // PT1H71M
Feed to Joda-Time.
Period period = Period.parse( d );
Normalize the minutes.
Period p = period.normalizedStandard();
Alternatively, you could tear apart the hours and minutes portions of your input, then feed them as ints to a constructor of Period.