Wyodrębnij czas z Ciągu daty

Jak sformatować "2010-07-14 09:00:02" ciąg daty, aby przedstawić tylko "9:00"?

Author: BalusC, 2010-08-17

7 answers

Użycie SimpleDateFormat Aby przekonwertować między ciągiem daty A rzeczywistym Date obiekt. z Date jako punktem wyjścia, możesz łatwo zastosować formatowanie w oparciu o różne wzorce zdefiniowane w javadoc SimpleDateFormat (Kliknij niebieski link kodu dla Javadoc).

Oto przykład:

String originalString = "2010-07-14 09:00:02";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(originalString);
String newString = new SimpleDateFormat("H:mm").format(date); // 9:00
 114
Author: BalusC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-17 17:08:31
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2010-07-14 09:00:02");
String time = new SimpleDateFormat("H:mm").format(date);

Http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

 14
Author: Adam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-17 17:10:54

Bardzo prostym sposobem jest użycie Formatter (Zobacz konwersje czasu daty) lub bardziej bezpośrednio String.format jak w

String.format("%tR", new Date())
 12
Author: Petr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-15 18:57:44

Zakładam, że twój pierwszy ciąg znaków jest rzeczywistym obiektem daty, popraw mnie, jeśli się mylę. Jeśli Tak, użyj obiektu SimpleDateFormat: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html . łańcuch formatu "H: mm" powinien się tym zająć.

 4
Author: MikeTheReader,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-17 17:08:39

Jeśli masz datę w liczbach całkowitych, możesz użyć jak tutaj:

Date date = new Date();
date.setYear(2010);
date.setMonth(07);
date.setDate(14)
date.setHours(9);
date.setMinutes(0);
date.setSeconds(0);
String time = new SimpleDateFormat("HH:mm:ss").format(date);
 3
Author: Bohdan Kolesnyk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-23 16:09:12

Inne odpowiedzi były dobre, gdy zadano pytanie. Czas płynie dalej, Date i SimpleDateFormat zostają zastąpione przez nowsze i lepsze klasy i wychodzą z użycia. W 2017 roku użyj klas w pakiecie java.time:

    String timeString = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))
            .format(DateTimeFormatter.ofPattern("H:mm"));

Wynik jest pożądany, 9:00.

 3
Author: Ole V.V.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-03 20:05:51
let datestring = "2017-02-14 02:16:28"

let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.full
formatter.timeStyle = DateFormatter.Style.full

formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

let date = formatter.date(from: datestring)
let date2 = formatter.String(from: date)
 0
Author: BHAVIK PANCHAL,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-17 07:19:00