使用其他语言设置日期

时间:2015-06-02 11:37:44

标签: android date

我的RSS新闻出了问题,我得到了所有新闻,但我想用我的语言(法语)设置日期,那么怎么做呢?

NewsAdapter.java:

public class NewsAdapter extends BaseAdapter {
    List<News> news;
    Context context;

    public NewsAdapter(Context context, List<News> news) {
        this.news = news;
        this.context = context;
    }

    public void update(List<News> news) {
        this.news = news;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return news.size();
    }

    @Override
    public Object getItem(int arg0) {
        return news.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        int screenSize = context.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK;

        int titleSize;
        switch(screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                titleSize = 26;
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                titleSize = 24;
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                titleSize = 15;
                break;
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                titleSize = 13;
                break;
            default:
                titleSize = 13;
        }
        int textSize;
        switch(screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                textSize = 25;
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                textSize = 23;
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                textSize = 14;
                break;
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                textSize = 12;
                break;
            default:
                textSize = 12;
        }
        LayoutInflater li = LayoutInflater.from(context);
        View v = li.inflate(R.layout.three_line_list_item, null);
        TextView tv1 = (TextView)v.findViewById(android.R.id.text1);
        tv1.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/GothamBold.ttf"));
        tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleSize);
        TextView tv3 = (TextView)v.findViewById(android.R.id.title);
        tv3.setTextColor(Color.parseColor("#2B729F"));
        tv3.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        tv3.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/GothamBook.ttf"));
        TextView tv2 = (TextView)v.findViewById(android.R.id.text2);
        tv2.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/GothamBook.ttf"));
        tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        News n = (News)getItem(arg0);
        tv1.setText(n.getTitle());
        String pubDate =  Html.fromHtml(n.getPubDate()).toString().replace((char) 65532, (char) 32).trim();
        tv3.setText(pubDate);
        String content = Html.fromHtml(n.getContent()).toString().replace((char) 65532, (char) 32).trim();
        tv2.setText(content);
        return v;
    }
}

RssParser.java:

public class RssParser {
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }
    private static final String ns = null;

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }

    public ArrayList<News> parse(InputStream in) throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        ArrayList<News> entries = new ArrayList<News>();

        parser.require(XmlPullParser.START_TAG, ns, "rss");
        parser.nextTag();
        parser.require(XmlPullParser.START_TAG, ns, "channel");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("item")) {
                String title = "";
                String link = "";
                String pubDate = "";
                String content = "";
                while (parser.next() != XmlPullParser.END_TAG) {
                    if (parser.getEventType() != XmlPullParser.START_TAG) {
                        continue;
                    }
                    name = parser.getName();
                    if (name.equals("title")) {
                        title = readText(parser);
                    } else if (name.equals("link")) {
                        link = readText(parser);
                    } else if (name.equals("pubDate")) {
                        pubDate = readText(parser);
                    } else if (name.equals("description")) {
                        content = readText(parser);
                    } else
                        skip(parser);
                }
                entries.add(new News(link, title, pubDate, content));
            }  else
                skip(parser);
        }
        return entries;
    }
}

Actualités.java(新闻):

public class Actualites extends android.support.v4.app.Fragment{
    AsyncTask<Void, Void, List> a = null;
    NewsAdapter adapter = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.activity_actualites, container, false);
        ListView list = (ListView) view.findViewById(R.id.listView1);
        adapter = new NewsAdapter(getActivity(), new ArrayList<News>());
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) adapter.getItem(position);
                startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(news.getLink())));
            }
        });
        a = new AsyncTask<Void, Void, List>() {
            @Override
            protected List doInBackground(Void... params) {
                ArrayList<News> res = new ArrayList<News>();
                try {
                    URL url = new URL("http://www.....fr/...,rss.html");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    RssParser parser = new RssParser();
                    try {
                        return parser.parse(urlConnection.getInputStream());
                    } catch (XmlPullParserException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return res;
            }

            @Override
            protected void onPostExecute(List result) {
                adapter.update(result);
            }
        };
        a.execute();
        TextView textView =(TextView)getActivity().findViewById(R.id.main_toolbar_title);
        textView.setText(getString(R.string.title_actu));
        textView.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        return view;
    }
}

我在xml rss中的日期是英文: 星期四,2015年4月9日15:21:51 +0000

那么如何用法语“解析”呢?

编辑----------------------

    String pubDate = Html.fromHtml(n.getPubDate()).toString().replace((char) 65532, (char) 32).trim();
    convertDateTimeZoneStr(pubDate);
    tv3.setText(pubDate);
    String content = Html.fromHtml(n.getContent()).toString().replace((char) 65532, (char) 32).trim();
    tv2.setText(content);
    return v;
}

public static String convertDateTimeZoneStr(String ddMMyyyy) throws ParseException {
    try{
        Date _date = new SimpleDateFormat("dd/MM/yyyy").parse(ddMMyyyy);
        SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        _simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
        return _simpleDateFormat.format(_date);
    }
    catch (ParseException e){
        e.printStackTrace();
    }
}

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

这里示例如何解析.U可以使用SimpleDateFormat

String dtStart = "yourdate";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

date = format.parse(dtStart);
System.out.println("Date ->" + date);

答案 1 :(得分:0)

以下代码是我一直在使用的示例:

public static String convertDateTimeZoneStr (String ddMMyyyy) throws ParseException {

    Date _date = new SimpleDateFormat("dd/MM/yyyy").parse(ddMMyyyy);

    SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    _simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    return _simpleDateFormat.format(_date);

}

您可以将TimeZone.getTimeZone("GMT")更改为您想要的任何内容。请参阅TimeZone Ids https://gist.github.com/arpit/1035596的此列表。

有关如何格式化的详细信息,请查看此链接http://developer.android.com/reference/java/text/SimpleDateFormat.html

编辑:

String pubDate = Html.fromHtml(n.getPubDate()).toString().replace((char) 65532, (char) 32).trim();

try{
    tv3.setText(convertDateTimeZoneStr(pubDate));
}
catch(ParseException e){
    // do whatever
}

String content = Html.fromHtml(n.getContent()).toString().replace((char) 65532, (char) 32).trim();
tv2.setText(content);