Skip to content

Instantly share code, notes, and snippets.

@haerulmuttaqin
Created September 11, 2018 07:22
Show Gist options
  • Save haerulmuttaqin/3d20a9127070451ccedd80c284649fd6 to your computer and use it in GitHub Desktop.
Save haerulmuttaqin/3d20a9127070451ccedd80c284649fd6 to your computer and use it in GitHub Desktop.
News App Utils
package com.haerul.newsapps;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import org.ocpsoft.prettytime.PrettyTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
public class Utils {
public static ColorDrawable[] vibrantLightColorList =
{
new ColorDrawable(Color.parseColor("#ffeead")),
new ColorDrawable(Color.parseColor("#93cfb3")),
new ColorDrawable(Color.parseColor("#fd7a7a")),
new ColorDrawable(Color.parseColor("#faca5f")),
new ColorDrawable(Color.parseColor("#1ba798")),
new ColorDrawable(Color.parseColor("#6aa9ae")),
new ColorDrawable(Color.parseColor("#ffbf27")),
new ColorDrawable(Color.parseColor("#d93947"))
};
public static ColorDrawable getRandomDrawbleColor() {
int idx = new Random().nextInt(vibrantLightColorList.length);
return vibrantLightColorList[idx];
}
public static String DateToTimeFormat(String oldstringDate){
PrettyTime p = new PrettyTime(new Locale(getCountry()));
String isTime = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
Locale.ENGLISH);
Date date = sdf.parse(oldstringDate);
isTime = p.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return isTime;
}
public static String DateFormat(String oldstringDate){
String newDate;
SimpleDateFormat dateFormat = new SimpleDateFormat("E, d MMM yyyy", new Locale(getCountry()));
try {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(oldstringDate);
newDate = dateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
newDate = oldstringDate;
}
return newDate;
}
public static String getCountry(){
Locale locale = Locale.getDefault();
String country = String.valueOf(locale.getCountry());
return country.toLowerCase();
}
}
@Sougi
Copy link

Sougi commented Jan 20, 2020

where is getLanguage() ; ... ????

@carlaponte
Copy link

Can't find the getLanguage ()

@StasKorol
Copy link

Where is getLanguage() method&?

@AmrAshraf-dev
Copy link

For all who asks about getLanguage method :

public static String getLanguage(){
Locale locale = Locale.getDefault();
String language = String.valueOf(locale.getLanguage());
return language;
}

@drparadox99
Copy link

Thanks rAshraf-dev !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment