java从字符串中得到数字

假设我输入2017-5-23 9:45:56,怎么得到2017,5,23,9,45,56.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Untitled {
    public static void main(String[] args) {
        String source = "2017-5-23 9:45:56";
        String regex = "\\d+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);
        while (matcher.find()) {
            int x = Integer.parseInt(matcher.group());
            System.out.println(x);
        }
    }
}

2017
5
23
9
45
56

如果问题得到解决,请点我回答左上角的采纳和向上的箭头,谢谢

循环遍历字符串,把大于等于0小于等于9的字符摘出到新的变量储存里面

可以用substring()方法截取字符串

若确定是时间字符串,可以用SimpleDateFormat、Date转为Calendar类型;
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);//获取年份
int month=cal.get(Calendar.MONTH);//获取月份
int day=cal.get(Calendar.DATE);//获取日
int hour=cal.get(Calendar.HOUR);//小时
int minute=cal.get(Calendar.MINUTE);//分
int second=cal.get(Calendar.SECOND);//秒

日期格式与字符串的转化,然后用日历分别获取对应的年月日时分秒