benjyspider benjyspider - 5 months ago 11
Android Question

Setup date in a different language

I got a problem with my RSS News, I get all of the news but I want to setup the date in my language (French) so how to do it?

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 (news):

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;
}
}


The date I get in the xml rss is in english:
Thu, 09 Apr 2015 15:21:51 +0000

So how to "parse" it in french ?

EDIT ----------------------

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

Answer

The following code is an example of what I've been using:

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);

}

You can change TimeZone.getTimeZone("GMT") to whatever you want. See this list of TimeZone Ids https://gist.github.com/arpit/1035596.

For more information about how to format, please take a look this link http://developer.android.com/reference/java/text/SimpleDateFormat.html

Edit:

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);
Comments