-2

I have hour and minute in edittext.(say for example 10:50)

how to get today's DateTime(like yyyy-MM-dd HH:mm:ss) based on above edit text value 10:50

UPDATE: this worked for me:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);

thank you all

sai
  • 97
  • 1
  • 10
  • Do I get that correctly? The user enters a time of day, say 10:50 — using a 24 hour clock? From that you need to get today’s date at the time entered, for example 2017-09-06 10:50:00 — right? – Ole V.V. Sep 06 '17 at 08:15

4 Answers4

2

EDIT: take a look at this other stackoverflow post about why one should prefer using the Java 8 java.time classes over Calendar or Date

In your instance, in order to parse an input in the form "HH:mm", you can create a DateTimeFormatter and use the static method LocalTime.parse(inputString, dateTimeFormatter) to get a LocalTime object

For example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(timeInputString, formatter);

Take a look at the Java documentation for DateTimeFormatter for more information on the patterns for years, days, or whatever.

If you're wanting to get both the date and time from a string such as "10:50", you won't be able to using the LocalDateTime.parse method because it can't obtain the date from an "HH:mm" string. So the way around this is to create the time off of LocalTime.parse as shown above, and then get the current date using the static method LocalDate.now(); And then combine those two results into a LocalDateTime object using it's static factory method LocalDateTime.of(date, time)

For example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(timeInputString, formatter);
LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);
Matthew Meacham
  • 403
  • 3
  • 9
  • 1
    Well explained answer, thank you. I would prefer to pass a time zone to `LocalDate.now()`, using the overloaded version that accepts a `ZoneId`, to make it explicit that we are asking for today’s date on a specific spot on earth. One may use for example `ZoneId.of("Africa/Banjul")` (substituting the relevant region and city) or `ZoneId.systemDefault()` (to use the device’s time zone setting). – Ole V.V. Sep 06 '17 at 08:31
  • You can use the Java 8 `java.time` classes with Android Java 7. It’s all explained very nicely in [this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Sep 06 '17 at 08:38
1
   Calendar cal = new Calendar.getInstance();// today's date 
 cal.set(Calendar.HOUR, 10);
 cal.set(Calendar.MINUTE, 50);
 cal.set(Calendar.SECOND, 0);
        System.out.println(cal.getTime()); //see the result

now , it's up to you to put them directly in cal.set with variables (not manually writing 10 ,50 , it's up to your creativity )

And this answer is with Calendar because you tagged Calendar in your question , you can also use something else than Calender

DetchKing
  • 185
  • 1
  • 12
  • the difficulty was how to assign hour and min to cal.set. 10 and 50 are just example, those edittext values are dynamic – sai Sep 06 '17 at 00:38
  • you said edittext, so he will enter 10 in the first edittext , then 50 in the second ! so those 2 edittext can be related to variables, then you use the variables in the set . the best way is to use an existing time api, so he will choose the hour and the minutes (so he can't for exemple enter: 23 hour, 70 minutes) then you catch them with the getters of the clock . – DetchKing Sep 06 '17 at 10:25
  • yes good explaination. I had to change the design. When on clicking the edittext with HH:mm value, i had to open TimePicketDialog and select the appropriate time. on selecting time from TimePicker it give the dateTime of the selected value, so it helped – sai Sep 06 '17 at 18:51
0

you just need use this function.

// hourMintText = "hh:mm"
private String getTodayAsFormat(String hourMintText){
    String[] hourMinAsArray = hourMintText.split(":");
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourMinAsArray[0]));
    calendar.set(Calendar.MINUTE, Integer.parseInt(hourMinAsArray[1]));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm", Locale.ENGLISH);

    return simpleDateFormat.format(calendar.getTimeInMillis());
}
-1

Try this:

String dateTime = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss aa", Locale.getDefault())
                        .format(new Date());
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Tsur Yohananov
  • 533
  • 1
  • 7
  • 17