android - 如何将字符串时间戳从公历转换为 shamsi

标签 android

你好,我正在从服务器获取公历时间戳,看起来像这样

{
  "error": false,
  "users": [
    {
      "id": 1,
      "username": "dfsg",
      "active": 1,
      "created_at": "2017-02-14 00:13:25"
    },
    {
      "id": 2,
      "username": "asf",
      "active": 1,
      "created_at": "2017-02-14 13:42:26"
    },
    {
      "id": 3,
      "username": "test2",
      "active": 1,
      "created_at": "2017-02-15 13:57:31"
    }
  ]
}

我想把它改成 shamsi 应该是这样的 1395-11-26 13:42:26 有什么课可以做吗?谢谢

最佳答案

你可以在你的项目中添加这个类,source

import java.text.SimpleDateFormat;
import java.util.Date;

public class PersianDate {
    public String todayShamsi() 
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String curentDateandTime = sdf.format(new Date());
        String year = curentDateandTime.substring(0, 4);
        String month = curentDateandTime.substring(4, 6);
        String day = curentDateandTime.substring(6, 8);
        int Y = Integer.valueOf(year);
        int M = Integer.valueOf(month);
        int D = Integer.valueOf(day);
        return Shamsi(Y, M, D);
    }
    public static String Shamsi(int Y, int M, int D)
    {
        if (Y == 0)
            Y = 2000;
        if (Y < 100)
            Y = Y + 1900;
        if (Y == 2000)
        {
            if (M > 2)
            {
                SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd");
                String curentDateandTime = temp.format(new Date());
                String year = curentDateandTime.substring(0, 4);
                String month = curentDateandTime.substring(4, 6);
                String day = curentDateandTime.substring(6, 8);
                Y = Integer.valueOf(year);
                M = Integer.valueOf(month);
                D = Integer.valueOf(day);
            }
        }
        if (M < 3 || (M == 3 && D < 21))
        {
            Y -= 622;
        }
        else Y -= 621;
        switch (M)
        {
        case 1: if (D < 21)
        {
            M = 10;
            D = D + 10;
        }
        else
        {
            M = 11;
            D -= 20;
        }
        break;
        case 2: if (D < 20)
        {
            M = 11;
            D = D + 11;
        }
        else
        {
            M = 12;
            D -= 19;
        }
        break;
        case 3:
            if (D < 21)
            {
                M = 12;
                D = D + 9;
            }
            else
            {
                M = 1;
                D -= 20;
            }
            break;
        case 4:
            if (D < 21)
            {
                M = 1;
                D = D + 11;
            }
            else
            {
                M = 2; D = D - 20;
            }
            break;
        case 5:
            if (D < 22)
            {
                M = M - 3;
                D = D + 10;
            }
            else
            {
                M = M - 2;
                D = D - 21;
            }
            break;
        case 6:
            if (D < 22)
            {
                M = M - 3;
                D = D + 10;
            }
            else
            {
                M = M - 2;
                D = D - 21;
            }
            break;
        case 7:
            if (D < 23)
            {
                M = M - 3;
                D = D + 9;
            }
            else
            {
                M = M - 2;
                D = D - 22;
            }
            break;
        case 8:
            if (D < 23)
            {
                M = M - 3;
                D = D + 9;
            }
            else
            {
                M = M - 2;
                D = D - 22;
            }
            break;
        case 9:
            if (D < 23)
            {
                M = M - 3;
                D = D + 9;
            }
            else
            {
                M = M - 2;
                D = D - 22;
            }
            break;
        case 10:
            if (D < 23)
            {
                M = 7;
                D = D + 8;
            }
            else
            {
                M = 8;
                D = D - 22;
            }
            break;
        case 11:
            if (D < 22)
            {
                M = M - 3;
                D = D + 9;
            }
            else
            {
                M = M - 2;
                D = D - 21;
            }
            break;
        case 12:
            if (D < 22)
            {
                M = M - 3;
                D = D + 9;
            }
            else
            {
                M = M - 2;
                D = D - 21;
            }
            break;
        }
        String month = "00";
        String day = "00";
        //D = Integer.valueOf(D)+1;
        if (M < 10)
        {
            month = "0" + M;
        }
        else
        {
            month = String.valueOf(M);
        }
        if (D < 10)
        {
            day = "0" + D;
        }
        else
        {
            day = String.valueOf(D);
        }
        return String.valueOf(Y) + "/" + month + "/" + day;
    }
} 

关于android - 如何将字符串时间戳从公历转换为 shamsi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250169/

相关文章:

android - 应用程序崩溃后相机未释放

android - 如何在向上滚动 ListView 时隐藏工具栏? (就像谷歌游戏商店)

java - Android Tabhost刷新

android - 捕获剪贴板的粘贴事件

android - Ksoap2 Android 添加属性到一个简单的属性

android - 如何知道Realm对象的创建线程

android - Android 版 YouTube channel 订阅

java - 想先找到焦距,然后使用 opencv android 实时检测到人脸的距离

android - 如何在 Android 应用程序中使用 Google autocomplete places api

android - 使用 XML 中的 ImageView 在 android 中平铺图像