-1

I don't understand how work java SimpleDateFormat. I want to show date and time by Locale.

For example

Locale.setDefault(Locale.FRANCE);
DateFormat correctDateFormatter =SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT, Locale.getDefault());
correctDateFormatter.format(date);

So I wait to output in 24-h format like

23:00

but I get it like

11 PM

How I can show time and date depends on the Local and do no not use time pattern("hh:mm" and so on)?

Lavan
  • 157
  • 9
  • 2
    https://stackoverflow.com/a/18734471/8089931 – Vishal Bhut Oct 04 '17 at 09:43
  • Please see my answer to display Locale based time – PEHLAJ Oct 04 '17 at 10:12
  • please look my question again – Lavan Oct 04 '17 at 10:20
  • If you just call `getTimeInstance(SimpleDateFormat.SHORT)`, it'll use the specific format for the device's default locale. Is that what you need? –  Oct 04 '17 at 12:02
  • Even on Android it’s certainly worth considering skipping the long outdated and notoriously troublesome `SimpleDateFormat` class and using the modern Java date and time API instead. This comes for Android in the ThreeTenABP library project, see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 04 '17 at 14:16
  • I cannot reproduce. When I run your code on my Java 8, I get `23:00` as you said you had expected. – Ole V.V. Oct 04 '17 at 14:20

5 Answers5

2
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CheckDate {

    public static void main(String[] args) {

        Date date = new Date();
        Locale.setDefault(Locale.FRANCE);
        SimpleDateFormat correctDateFormatter = new SimpleDateFormat("HH:mm", Locale.FRANCE);
        correctDateFormatter.format(date);
        System.out.println("24 hrs format :: "+correctDateFormatter.format(date));
    }

}

Otherwise if you change your system date as 24hrs format it will come in program also

Vijay
  • 93
  • 1
  • 8
1

This will return the format you specified in the timezone you need:

String timeString = SimpleDateFormat("HH:mm", Locale.FRANCE).format(date);
Endre Börcsök
  • 477
  • 1
  • 8
  • 19
1
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.FRANCE);

H is used for 24 hour and h 12 hour format.

Ankita
  • 1,129
  • 1
  • 8
  • 15
1

Try String formattedLocalTime = getFormattedLocalTimeFromUtc("2016-02-16T04:46:28Z", "MM/dd/yyyy");

final String LOCAL_TIME_FORMAT = "EEE MMM dd HH:mm:ss z yyyy"; //This is your local date format
final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; //This is input date format
final String TIMEZONE_UTC = "UTC";

public static String getFormattedLocalTimeFromUtc(String utcTimeStamp, String outputFormat) {

    String formattedTime = null;
    if(!TextUtils.isEmpty(utcTimeStamp)) {

            String localTime = null;
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
            sdf.setTimeZone(TimeZone.getTimeZone(TIMEZONE_UTC));
            try {
                localTime = sdf.parse(utcTimeStamp).toString();
            }
            catch(ParseException e) {
                e.printStackTrace();
            }

            DateFormat inputDateFormat = new SimpleDateFormat(LOCAL_TIME_FORMAT);
            inputDateFormat.setTimeZone(TimeZone.getTimeZone(TIMEZONE_UTC));
            Date date = null;
            try {
                date = inputDateFormat.parse(localTime);
            }
            catch(ParseException e) {
                e.printStackTrace();
            }

            DateFormat outputDateFormat = new SimpleDateFormat(outputFormat);
            formattedTime = outputDateFormat.format(date);
        }
    }
    return formattedTime;
}

HH.mm format for time

H: Hour in day (0-23) h: Hour in am/pm (0-11)

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
1

Check the SDF documentation here

Where there are various types of characters you can use to format your date,

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.FRANCE);

Once you have this:

simpledateFormat.format(date);
MadScientist
  • 2,134
  • 14
  • 27