gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:03:24 24 4
gpt4 key购买 nike

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

{
"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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com